Skip to content

Commit d28d2bb

Browse files
authored
prepare for #210 (#325)
1 parent 0ef6b18 commit d28d2bb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+491
-464
lines changed

.github/workflows/go.yml

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,62 @@ jobs:
1414
matrix:
1515
# Locked at https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on
1616
os:
17-
- ubuntu-18.04
17+
- ubuntu-20.04
1818
- windows-2019
19+
- macos-10.15
1920
runs-on: ${{ matrix.os }}
21+
defaults:
22+
run:
23+
shell: bash
2024
steps:
21-
- uses: actions/checkout@v2
2225

2326
- uses: actions/setup-go@v2
2427
with:
2528
go-version: 1.x
26-
- run: go version
29+
30+
- id: go-cache-paths
31+
run: |
32+
echo "::set-output name=go-build::$(go env GOCACHE)"
33+
echo "::set-output name=go-mod::$(go env GOMODCACHE)"
34+
35+
- name: Go Build Cache
36+
uses: actions/cache@v2
37+
with:
38+
path: ${{ steps.go-cache-paths.outputs.go-build }}
39+
key: ${{ runner.os }}-go-build-${{ hashFiles('**/go.sum') }}
40+
41+
- name: Go Mod Cache
42+
uses: actions/cache@v2
43+
with:
44+
path: ${{ steps.go-cache-paths.outputs.go-mod }}
45+
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}
46+
47+
48+
- uses: actions/checkout@v2
2749

2850
- run: go mod download && go mod tidy && go mod verify
29-
- run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]
30-
shell: bash
51+
- if: runner.os == 'Linux'
52+
run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]
3153

3254
- run: go vet ./...
33-
- run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]
34-
shell: bash
55+
- if: runner.os == 'Linux'
56+
run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]
3557

3658
- run: go fmt ./...
37-
- run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]
38-
shell: bash
59+
- if: runner.os == 'Linux'
60+
run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]
3961

4062
- run: go test ./...
41-
- run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]
42-
shell: bash
63+
- if: runner.os == 'Linux'
64+
run: git --no-pager diff && [[ $(git --no-pager diff --name-only | wc -l) = 0 ]]
65+
66+
67+
- if: runner.os == 'Linux'
68+
name: Errors must not be capitalized https://github.com/golang/go/wiki/CodeReviewComments#error-strings
69+
run: |
70+
! git grep -E '(fmt|errors)[^(]+\(.[A-Z]'
71+
72+
- if: runner.os == 'Linux'
73+
name: Did you mean %q
74+
run: |
75+
! git grep -E "'[%].'"

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Here's some projects that depend on _kin-openapi_:
4444

4545
# Some recipes
4646
## Loading OpenAPI document
47-
Use `SwaggerLoader`, which resolves all JSON references:
47+
Use `SwaggerLoader`, which resolves all references:
4848
```go
4949
swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromFile("swagger.json")
5050
```
@@ -108,9 +108,7 @@ func main() {
108108
responseValidationInput := &openapi3filter.ResponseValidationInput{
109109
RequestValidationInput: requestValidationInput,
110110
Status: respStatus,
111-
Header: http.Header{
112-
"Content-Type": []string{respContentType},
113-
},
111+
Header: http.Header{"Content-Type": []string{respContentType}},
114112
}
115113
if respBody != nil {
116114
data, _ := json.Marshal(respBody)
@@ -160,7 +158,7 @@ func xmlBodyDecoder(body []byte) (interface{}, error) {
160158
}
161159
```
162160

163-
## Custom function for check uniqueness of JSON array
161+
## Custom function to check uniqueness of array items
164162

165163
By defaut, the library check unique items by below predefined function
166164

@@ -169,8 +167,6 @@ func isSliceOfUniqueItems(xs []interface{}) bool {
169167
s := len(xs)
170168
m := make(map[string]struct{}, s)
171169
for _, x := range xs {
172-
// The input slice is coverted from a JSON string, there shall
173-
// have no error when covert it back.
174170
key, _ := json.Marshal(&x)
175171
m[string(key)] = struct{}{}
176172
}
@@ -180,7 +176,7 @@ func isSliceOfUniqueItems(xs []interface{}) bool {
180176

181177
In the predefined function using `json.Marshal` to generate a string can
182178
be used as a map key which is to support check the uniqueness of an array
183-
when the array items are JSON objects or JSON arraies. You can register
179+
when the array items are objects or arrays. You can register
184180
you own function according to your input data to get better performance:
185181

186182
```go
@@ -194,7 +190,7 @@ func main() {
194190
}
195191

196192
func arrayUniqueItemsChecker(items []interface{}) bool {
197-
// Check the uniqueness of the input slice(array in JSON)
193+
// Check the uniqueness of the input slice
198194
}
199195
```
200196

jsoninfo/marshal.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ func (encoder *ObjectEncoder) EncodeStructFieldsAndExtensions(value interface{})
5959
// Follow "encoding/json" semantics
6060
if reflection.Kind() != reflect.Ptr {
6161
// Panic because this is a clear programming error
62-
panic(fmt.Errorf("Value %s is not a pointer", reflection.Type().String()))
62+
panic(fmt.Errorf("value %s is not a pointer", reflection.Type().String()))
6363
}
6464
if reflection.IsNil() {
6565
// Panic because this is a clear programming error
66-
panic(fmt.Errorf("Value %s is nil", reflection.Type().String()))
66+
panic(fmt.Errorf("value %s is nil", reflection.Type().String()))
6767
}
6868

6969
// Take the element
@@ -146,7 +146,7 @@ iteration:
146146
continue iteration
147147
}
148148
default:
149-
panic(fmt.Errorf("Field '%s' has unsupported type %s", field.JSONName, field.Type.String()))
149+
panic(fmt.Errorf("field %q has unsupported type %s", field.JSONName, field.Type.String()))
150150
}
151151

152152
// No special treament is needed

jsoninfo/unmarshal.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type ObjectDecoder struct {
2525
func NewObjectDecoder(data []byte) (*ObjectDecoder, error) {
2626
var remainingFields map[string]json.RawMessage
2727
if err := json.Unmarshal(data, &remainingFields); err != nil {
28-
return nil, fmt.Errorf("Failed to unmarshal extension properties: %v\nInput: %s", err, data)
28+
return nil, fmt.Errorf("failed to unmarshal extension properties: %v (%s)", err, data)
2929
}
3030
return &ObjectDecoder{
3131
Data: data,
@@ -41,18 +41,18 @@ func (decoder *ObjectDecoder) DecodeExtensionMap() map[string]json.RawMessage {
4141
func (decoder *ObjectDecoder) DecodeStructFieldsAndExtensions(value interface{}) error {
4242
reflection := reflect.ValueOf(value)
4343
if reflection.Kind() != reflect.Ptr {
44-
panic(fmt.Errorf("Value %T is not a pointer", value))
44+
panic(fmt.Errorf("value %T is not a pointer", value))
4545
}
4646
if reflection.IsNil() {
47-
panic(fmt.Errorf("Value %T is nil", value))
47+
panic(fmt.Errorf("value %T is nil", value))
4848
}
4949
reflection = reflection.Elem()
5050
for (reflection.Kind() == reflect.Interface || reflection.Kind() == reflect.Ptr) && !reflection.IsNil() {
5151
reflection = reflection.Elem()
5252
}
5353
reflectionType := reflection.Type()
5454
if reflectionType.Kind() != reflect.Struct {
55-
panic(fmt.Errorf("Value %T is not a struct", value))
55+
panic(fmt.Errorf("value %T is not a struct", value))
5656
}
5757
typeInfo := GetTypeInfo(reflectionType)
5858

@@ -87,7 +87,7 @@ func (decoder *ObjectDecoder) DecodeStructFieldsAndExtensions(value interface{})
8787
continue
8888
}
8989
}
90-
return fmt.Errorf("Error while unmarshalling property '%s' (%s): %v",
90+
return fmt.Errorf("failed to unmarshal property %q (%s): %v",
9191
field.JSONName, fieldValue.Type().String(), err)
9292
}
9393
if !isPtr {
@@ -109,7 +109,7 @@ func (decoder *ObjectDecoder) DecodeStructFieldsAndExtensions(value interface{})
109109
continue
110110
}
111111
}
112-
return fmt.Errorf("Error while unmarshalling property '%s' (%s): %v",
112+
return fmt.Errorf("failed to unmarshal property %q (%s): %v",
113113
field.JSONName, fieldPtr.Type().String(), err)
114114
}
115115

jsoninfo/unmarshal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func TestDecodeStructFieldsAndExtensions(t *testing.T) {
149149
}{}
150150
err = d.DecodeStructFieldsAndExtensions(&value)
151151
require.Error(t, err)
152-
require.EqualError(t, err, "Error while unmarshalling property 'field1' (*int): json: cannot unmarshal string into Go value of type int")
152+
require.EqualError(t, err, `failed to unmarshal property "field1" (*int): json: cannot unmarshal string into Go value of type int`)
153153
require.Equal(t, 0, value.Field1)
154154
require.Equal(t, 2, len(d.DecodeExtensionMap()))
155155
})

jsoninfo/unsupported_properties_error.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66
"sort"
7-
"strings"
87
)
98

109
// UnsupportedPropertiesError is a helper for extensions that want to refuse
@@ -27,7 +26,7 @@ func (err *UnsupportedPropertiesError) Error() string {
2726
m := err.UnsupportedProperties
2827
typeInfo := GetTypeInfoForValue(err.Value)
2928
if m == nil || typeInfo == nil {
30-
return "Invalid UnsupportedPropertiesError"
29+
return fmt.Sprintf("invalid %T", *err)
3130
}
3231
keys := make([]string, 0, len(m))
3332
for k := range m {
@@ -36,10 +35,8 @@ func (err *UnsupportedPropertiesError) Error() string {
3635
sort.Strings(keys)
3736
supported := typeInfo.FieldNames()
3837
if len(supported) == 0 {
39-
return fmt.Sprintf("Type '%T' doesn't take any properties. Unsupported properties: '%s'\n",
40-
err.Value, strings.Join(keys, "', '"))
38+
return fmt.Sprintf("type \"%T\" doesn't take any properties. Unsupported properties: %+v",
39+
err.Value, keys)
4140
}
42-
return fmt.Sprintf("Unsupported properties: '%s'\nSupported properties are: '%s'",
43-
strings.Join(keys, "', '"),
44-
strings.Join(supported, "', '"))
41+
return fmt.Sprintf("unsupported properties: %+v (supported properties are: %+v)", keys, supported)
4542
}

openapi2/openapi2.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func (pathItem *PathItem) GetOperation(method string) *Operation {
120120
case http.MethodPut:
121121
return pathItem.Put
122122
default:
123-
panic(fmt.Errorf("Unsupported HTTP method '%s'", method))
123+
panic(fmt.Errorf("unsupported HTTP method %q", method))
124124
}
125125
}
126126

@@ -141,7 +141,7 @@ func (pathItem *PathItem) SetOperation(method string, operation *Operation) {
141141
case http.MethodPut:
142142
pathItem.Put = operation
143143
default:
144-
panic(fmt.Errorf("Unsupported HTTP method '%s'", method))
144+
panic(fmt.Errorf("unsupported HTTP method %q", method))
145145
}
146146
}
147147

openapi2conv/openapi2_conv.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ func ToV3SecurityScheme(securityScheme *openapi2.SecurityScheme) (*openapi3.Secu
519519
case "password":
520520
flows.Password = flow
521521
default:
522-
return nil, fmt.Errorf("Unsupported flow '%s'", securityScheme.Flow)
522+
return nil, fmt.Errorf("unsupported flow %q", securityScheme.Flow)
523523
}
524524
}
525525
return &openapi3.SecuritySchemeRef{
@@ -1077,7 +1077,7 @@ func FromV3SecurityScheme(swagger *openapi3.Swagger, ref *openapi3.SecuritySchem
10771077
}
10781078
}
10791079
default:
1080-
return nil, fmt.Errorf("Unsupported security scheme type '%s'", securityScheme.Type)
1080+
return nil, fmt.Errorf("unsupported security scheme type %q", securityScheme.Type)
10811081
}
10821082
return result, nil
10831083
}

openapi3/discriminator_test.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ import (
66
"github.com/stretchr/testify/require"
77
)
88

9-
var jsonSpecWithDiscriminator = []byte(`
9+
func TestParsingDiscriminator(t *testing.T) {
10+
const spec = `
1011
{
1112
"openapi": "3.0.0",
13+
"info": {
14+
"version": "1.0.0",
15+
"title": "title",
16+
"description": "desc",
17+
"contact": {
18+
"email": "email"
19+
}
20+
},
21+
"paths": {},
1222
"components": {
1323
"schemas": {
1424
"MyResponseType": {
@@ -33,10 +43,14 @@ var jsonSpecWithDiscriminator = []byte(`
3343
}
3444
}
3545
}
36-
`)
46+
`
3747

38-
func TestParsingDiscriminator(t *testing.T) {
39-
loader, err := NewSwaggerLoader().LoadSwaggerFromData(jsonSpecWithDiscriminator)
48+
loader := NewSwaggerLoader()
49+
doc, err := loader.LoadSwaggerFromData([]byte(spec))
4050
require.NoError(t, err)
41-
require.Equal(t, 2, len(loader.Components.Schemas["MyResponseType"].Value.Discriminator.Mapping))
51+
52+
err = doc.Validate(loader.Context)
53+
require.NoError(t, err)
54+
55+
require.Equal(t, 2, len(doc.Components.Schemas["MyResponseType"].Value.Discriminator.Mapping))
4256
}

openapi3/encoding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (encoding *Encoding) Validate(c context.Context) error {
8686
sm.Style == SerializationDeepObject && sm.Explode:
8787
// it is a valid
8888
default:
89-
return fmt.Errorf("Serialization method with style=%q and explode=%v is not supported by media type", sm.Style, sm.Explode)
89+
return fmt.Errorf("serialization method with style=%q and explode=%v is not supported by media type", sm.Style, sm.Explode)
9090
}
9191

9292
return nil

0 commit comments

Comments
 (0)