Skip to content

Commit 2b52cd5

Browse files
fix(deps): update kin-openapi to v0.104.0 (#953)
Adapt to breaking changes from v0.103.0
1 parent f4cf8f9 commit 2b52cd5

File tree

13 files changed

+148
-119
lines changed

13 files changed

+148
-119
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module github.com/deepmap/oapi-codegen
22

33
require (
44
github.com/apapsch/go-jsonmerge/v2 v2.0.0
5-
github.com/getkin/kin-openapi v0.112.0
5+
github.com/getkin/kin-openapi v0.114.0
66
github.com/gin-gonic/gin v1.8.2
77
github.com/go-chi/chi/v5 v5.0.8
88
github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219
@@ -45,6 +45,7 @@ require (
4545
github.com/modern-go/reflect2 v1.0.2 // indirect
4646
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
4747
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
48+
github.com/perimeterx/marshmallow v1.1.4 // indirect
4849
github.com/pkg/errors v0.9.1 // indirect
4950
github.com/pmezard/go-difflib v1.0.0 // indirect
5051
github.com/stretchr/objx v0.5.0 // indirect

go.sum

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn
1010
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.0-20210816181553-5444fa50b93d/go.mod h1:tmAIfUFEirG/Y8jhZ9M+h36obRZAk/1fcSpXwAVlfqE=
1111
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc=
1212
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
13-
github.com/getkin/kin-openapi v0.112.0 h1:lnLXx3bAG53EJVI4E/w0N8i1Y/vUZUEsnrXkgnfn7/Y=
14-
github.com/getkin/kin-openapi v0.112.0/go.mod h1:QtwUNt0PAAgIIBEvFWYfB7dfngxtAaqCX1zYHMZDeK8=
13+
github.com/getkin/kin-openapi v0.114.0 h1:ar7QiJpDdlR+zSyPjrLf8mNnpoFP/lI90XcywMCFNe8=
14+
github.com/getkin/kin-openapi v0.114.0/go.mod h1:l5e9PaFUo9fyLJCPGQeXI2ML8c3P8BHOEV2VaAVf/pc=
1515
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
1616
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
1717
github.com/gin-gonic/gin v1.8.2 h1:UzKToD9/PoFj/V4rvlKqTRKnQYyz8Sc1MJlv4JHPtvY=
@@ -31,6 +31,8 @@ github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/j
3131
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
3232
github.com/go-playground/validator/v10 v10.11.1 h1:prmOlTVv+YjZjmRmNSF3VmspqJIxJWXmqUsHwfTRRkQ=
3333
github.com/go-playground/validator/v10 v10.11.1/go.mod h1:i+3WkQ1FvaUjjxh1kSvIA4dMGDBiPU55YFDl0WbKdWU=
34+
github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM=
35+
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
3436
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
3537
github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk=
3638
github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
@@ -102,6 +104,8 @@ github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwd
102104
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
103105
github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvIwycIU=
104106
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
107+
github.com/perimeterx/marshmallow v1.1.4 h1:pZLDH9RjlLGGorbXhcaQLhfuV0pFMNfPO55FuFkxqLw=
108+
github.com/perimeterx/marshmallow v1.1.4/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
105109
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
106110
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
107111
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

internal/test/components/components.gen.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/test/components/components.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ components:
208208
additionalProperties:
209209
$ref: '#/components/schemas/SchemaObject'
210210
OneOfObject1:
211-
description: oneOf with references and no disciminator
211+
description: oneOf with references and no discriminator
212212
oneOf:
213213
- $ref: '#/components/schemas/OneOfVariant1'
214214
- $ref: '#/components/schemas/OneOfVariant2'
@@ -244,7 +244,7 @@ components:
244244
- $ref: '#/components/schemas/OneOfVariant2'
245245
- $ref: '#/components/schemas/OneOfVariant3'
246246
OneOfObject5:
247-
description: oneOf with disciminator but no mapping
247+
description: oneOf with discriminator but no mapping
248248
oneOf:
249249
- $ref: '#/components/schemas/OneOfVariant4'
250250
- $ref: '#/components/schemas/OneOfVariant5'

internal/test/issues/issue-grab_import_names/issue.gen.go

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/codegen/codegen.go

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,12 @@ func Generate(spec *openapi3.T, opts Configuration) (string, error) {
201201

202202
var strictServerOut string
203203
if opts.Generate.Strict {
204-
responses, err := GenerateResponseDefinitions("", spec.Components.Responses)
205-
if err != nil {
206-
return "", fmt.Errorf("error generation response definitions for schema: %w", err)
204+
var responses []ResponseDefinition
205+
if spec.Components != nil {
206+
responses, err = GenerateResponseDefinitions("", spec.Components.Responses)
207+
if err != nil {
208+
return "", fmt.Errorf("error generation response definitions for schema: %w", err)
209+
}
207210
}
208211
strictServerResponses, err := GenerateStrictResponses(t, responses)
209212
if err != nil {
@@ -339,28 +342,31 @@ func Generate(spec *openapi3.T, opts Configuration) (string, error) {
339342
}
340343

341344
func GenerateTypeDefinitions(t *template.Template, swagger *openapi3.T, ops []OperationDefinition, excludeSchemas []string) (string, error) {
342-
schemaTypes, err := GenerateTypesForSchemas(t, swagger.Components.Schemas, excludeSchemas)
343-
if err != nil {
344-
return "", fmt.Errorf("error generating Go types for component schemas: %w", err)
345-
}
345+
var allTypes []TypeDefinition
346+
if swagger.Components != nil {
347+
schemaTypes, err := GenerateTypesForSchemas(t, swagger.Components.Schemas, excludeSchemas)
348+
if err != nil {
349+
return "", fmt.Errorf("error generating Go types for component schemas: %w", err)
350+
}
346351

347-
paramTypes, err := GenerateTypesForParameters(t, swagger.Components.Parameters)
348-
if err != nil {
349-
return "", fmt.Errorf("error generating Go types for component parameters: %w", err)
350-
}
351-
allTypes := append(schemaTypes, paramTypes...)
352+
paramTypes, err := GenerateTypesForParameters(t, swagger.Components.Parameters)
353+
if err != nil {
354+
return "", fmt.Errorf("error generating Go types for component parameters: %w", err)
355+
}
356+
allTypes = append(schemaTypes, paramTypes...)
352357

353-
responseTypes, err := GenerateTypesForResponses(t, swagger.Components.Responses)
354-
if err != nil {
355-
return "", fmt.Errorf("error generating Go types for component responses: %w", err)
356-
}
357-
allTypes = append(allTypes, responseTypes...)
358+
responseTypes, err := GenerateTypesForResponses(t, swagger.Components.Responses)
359+
if err != nil {
360+
return "", fmt.Errorf("error generating Go types for component responses: %w", err)
361+
}
362+
allTypes = append(allTypes, responseTypes...)
358363

359-
bodyTypes, err := GenerateTypesForRequestBodies(t, swagger.Components.RequestBodies)
360-
if err != nil {
361-
return "", fmt.Errorf("error generating Go types for component request bodies: %w", err)
364+
bodyTypes, err := GenerateTypesForRequestBodies(t, swagger.Components.RequestBodies)
365+
if err != nil {
366+
return "", fmt.Errorf("error generating Go types for component request bodies: %w", err)
367+
}
368+
allTypes = append(allTypes, bodyTypes...)
362369
}
363-
allTypes = append(allTypes, bodyTypes...)
364370

365371
// Go through all operations, and add their types to allTypes, so that we can
366372
// scan all of them for enums. Operation definitions are handled differently
@@ -893,6 +899,10 @@ func OperationImports(ops []OperationDefinition) (map[string]goImport, error) {
893899

894900
func GetTypeDefinitionsImports(swagger *openapi3.T, excludeSchemas []string) (map[string]goImport, error) {
895901
res := map[string]goImport{}
902+
if swagger.Components == nil {
903+
return res, nil
904+
}
905+
896906
schemaImports, err := GetSchemaImports(swagger.Components.Schemas, excludeSchemas)
897907
if err != nil {
898908
return nil, err

pkg/codegen/extension.go

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package codegen
22

33
import (
4-
"encoding/json"
54
"fmt"
65
)
76

@@ -22,15 +21,10 @@ const (
2221
)
2322

2423
func extString(extPropValue interface{}) (string, error) {
25-
raw, ok := extPropValue.(json.RawMessage)
24+
str, ok := extPropValue.(string)
2625
if !ok {
2726
return "", fmt.Errorf("failed to convert type: %T", extPropValue)
2827
}
29-
var str string
30-
if err := json.Unmarshal(raw, &str); err != nil {
31-
return "", fmt.Errorf("failed to unmarshal json: %w", err)
32-
}
33-
3428
return str, nil
3529
}
3630
func extTypeName(extPropValue interface{}) (string, error) {
@@ -42,53 +36,49 @@ func extParseGoFieldName(extPropValue interface{}) (string, error) {
4236
}
4337

4438
func extParseOmitEmpty(extPropValue interface{}) (bool, error) {
45-
raw, ok := extPropValue.(json.RawMessage)
39+
omitEmpty, ok := extPropValue.(bool)
4640
if !ok {
4741
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
4842
}
49-
50-
var omitEmpty bool
51-
if err := json.Unmarshal(raw, &omitEmpty); err != nil {
52-
return false, fmt.Errorf("failed to unmarshal json: %w", err)
53-
}
54-
5543
return omitEmpty, nil
5644
}
5745

5846
func extExtraTags(extPropValue interface{}) (map[string]string, error) {
59-
raw, ok := extPropValue.(json.RawMessage)
47+
tagsI, ok := extPropValue.(map[string]interface{})
6048
if !ok {
6149
return nil, fmt.Errorf("failed to convert type: %T", extPropValue)
6250
}
63-
var tags map[string]string
64-
if err := json.Unmarshal(raw, &tags); err != nil {
65-
return nil, fmt.Errorf("failed to unmarshal json: %w", err)
51+
tags := make(map[string]string, len(tagsI))
52+
for k, v := range tagsI {
53+
vs, ok := v.(string)
54+
if !ok {
55+
return nil, fmt.Errorf("failed to convert type: %T", v)
56+
}
57+
tags[k] = vs
6658
}
6759
return tags, nil
6860
}
6961

7062
func extParseGoJsonIgnore(extPropValue interface{}) (bool, error) {
71-
raw, ok := extPropValue.(json.RawMessage)
63+
goJsonIgnore, ok := extPropValue.(bool)
7264
if !ok {
7365
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
7466
}
75-
76-
var goJsonIgnore bool
77-
if err := json.Unmarshal(raw, &goJsonIgnore); err != nil {
78-
return false, fmt.Errorf("failed to unmarshal json: %w", err)
79-
}
80-
8167
return goJsonIgnore, nil
8268
}
8369

8470
func extParseEnumVarNames(extPropValue interface{}) ([]string, error) {
85-
raw, ok := extPropValue.(json.RawMessage)
71+
namesI, ok := extPropValue.([]interface{})
8672
if !ok {
8773
return nil, fmt.Errorf("failed to convert type: %T", extPropValue)
8874
}
89-
var names []string
90-
if err := json.Unmarshal(raw, &names); err != nil {
91-
return nil, fmt.Errorf("failed to unmarshal json: %w", err)
75+
names := make([]string, len(namesI))
76+
for i, v := range namesI {
77+
vs, ok := v.(string)
78+
if !ok {
79+
return nil, fmt.Errorf("failed to convert type: %T", v)
80+
}
81+
names[i] = vs
9282
}
9383
return names, nil
9484
}

pkg/codegen/extension_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
func Test_extTypeName(t *testing.T) {
1111
type args struct {
12-
extPropValue interface{}
12+
extPropValue json.RawMessage
1313
}
1414
tests := []struct {
1515
name string
@@ -24,21 +24,27 @@ func Test_extTypeName(t *testing.T) {
2424
wantErr: false,
2525
},
2626
{
27-
name: "type conversion error",
27+
name: "nil conversion error",
2828
args: args{nil},
2929
want: "",
3030
wantErr: true,
3131
},
3232
{
33-
name: "json unmarshal error",
34-
args: args{json.RawMessage("invalid json format")},
33+
name: "type conversion error",
34+
args: args{json.RawMessage(`12`)},
3535
want: "",
3636
wantErr: true,
3737
},
3838
}
3939
for _, tt := range tests {
4040
t.Run(tt.name, func(t *testing.T) {
41-
got, err := extTypeName(tt.args.extPropValue)
41+
// kin-openapi no longer returns these as RawMessage
42+
var extPropValue interface{}
43+
if tt.args.extPropValue != nil {
44+
err := json.Unmarshal(tt.args.extPropValue, &extPropValue)
45+
assert.NoError(t, err)
46+
}
47+
got, err := extTypeName(extPropValue)
4248
if tt.wantErr {
4349
assert.Error(t, err)
4450
return

pkg/codegen/merge_schemas.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,11 @@ func mergeOpenapiSchemas(s1, s2 openapi3.Schema, allOf bool) (openapi3.Schema, e
212212
if SchemaHasAdditionalProperties(&s1) && SchemaHasAdditionalProperties(&s2) {
213213
return openapi3.Schema{}, errors.New("merging two schemas with additional properties, this is unhandled")
214214
}
215-
if s1.AdditionalProperties != nil {
216-
result.AdditionalProperties = s1.AdditionalProperties
215+
if s1.AdditionalProperties.Schema != nil {
216+
result.AdditionalProperties.Schema = s1.AdditionalProperties.Schema
217217
}
218-
if s2.AdditionalProperties != nil {
219-
result.AdditionalProperties = s2.AdditionalProperties
218+
if s2.AdditionalProperties.Schema != nil {
219+
result.AdditionalProperties.Schema = s2.AdditionalProperties.Schema
220220
}
221221

222222
// Allow discriminators for allOf merges, but disallow for one/anyOfs.

pkg/codegen/operations.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ func (pd ParameterDefinition) GoVariableName() string {
126126

127127
func (pd ParameterDefinition) GoName() string {
128128
goName := pd.ParamName
129-
if _, ok := pd.Spec.ExtensionProps.Extensions[extGoName]; ok {
130-
if extGoFieldName, err := extParseGoFieldName(pd.Spec.ExtensionProps.Extensions[extGoName]); err == nil {
129+
if _, ok := pd.Spec.Extensions[extGoName]; ok {
130+
if extGoFieldName, err := extParseGoFieldName(pd.Spec.Extensions[extGoName]); err == nil {
131131
goName = extGoFieldName
132132
}
133133
}
@@ -825,12 +825,12 @@ func GenerateParamsTypes(op OperationDefinition) []TypeDefinition {
825825
})
826826
}
827827
prop := Property{
828-
Description: param.Spec.Description,
829-
JsonFieldName: param.ParamName,
830-
Required: param.Required,
831-
Schema: pSchema,
832-
NeedsFormTag: param.Style() == "form",
833-
ExtensionProps: &param.Spec.ExtensionProps,
828+
Description: param.Spec.Description,
829+
JsonFieldName: param.ParamName,
830+
Required: param.Required,
831+
Schema: pSchema,
832+
NeedsFormTag: param.Style() == "form",
833+
Extensions: param.Spec.Extensions,
834834
}
835835
s.Properties = append(s.Properties, prop)
836836
}

0 commit comments

Comments
 (0)