Skip to content

Commit

Permalink
v2Tov3: handle parameter schema refs (#455)
Browse files Browse the repository at this point in the history
Co-authored-by: Vincent Behar <v.behar@free.fr>
  • Loading branch information
fenollp and vbehar committed Dec 2, 2021
1 parent f1075be commit fd08c7f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
12 changes: 11 additions & 1 deletion openapi2conv/openapi2_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ func ToV3Parameter(components *openapi3.Components, parameter *openapi2.Paramete
required = true
}

var schemaRefRef string
if schemaRef := parameter.Schema; schemaRef != nil && schemaRef.Ref != "" {
schemaRefRef = schemaRef.Ref
}
result := &openapi3.Parameter{
In: parameter.In,
Name: parameter.Name,
Expand All @@ -322,7 +326,9 @@ func ToV3Parameter(components *openapi3.Components, parameter *openapi2.Paramete
AllowEmptyValue: parameter.AllowEmptyValue,
UniqueItems: parameter.UniqueItems,
MultipleOf: parameter.MultipleOf,
}}),
},
Ref: schemaRefRef,
}),
}
return &openapi3.ParameterRef{Value: result}, nil, nil, nil
}
Expand Down Expand Up @@ -980,6 +986,10 @@ func FromV3Parameter(ref *openapi3.ParameterRef, components *openapi3.Components
}
if schemaRef := parameter.Schema; schemaRef != nil {
schemaRef, _ = FromV3SchemaRef(schemaRef, components)
if ref := schemaRef.Ref; ref != "" {
result.Schema = &openapi3.SchemaRef{Ref: FromV3Ref(ref)}
return result, nil
}
schema := schemaRef.Value
result.Type = schema.Type
result.Format = schema.Format
Expand Down
77 changes: 77 additions & 0 deletions openapi2conv/openapi2_conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ const exampleV2 = `
"ItemExtension": {
"description": "It could be anything.",
"type": "boolean"
},
"foo": {
"description": "foo description",
"enum": [
"bar",
"baz"
],
"type": "string"
}
},
"externalDocs": {
Expand Down Expand Up @@ -305,6 +313,34 @@ const exampleV2 = `
},
"x-path": "path extension 1",
"x-path2": "path extension 2"
},
"/foo": {
"get": {
"operationId": "getFoo",
"consumes": [
"application/json",
"application/xml"
],
"parameters": [
{
"x-originalParamName": "foo",
"in": "body",
"name": "foo",
"schema": {
"$ref": "#/definitions/foo"
}
}
],
"responses": {
"default": {
"description": "OK",
"schema": {
"$ref": "#/definitions/foo"
}
}
},
"summary": "get foo"
}
}
},
"responses": {
Expand Down Expand Up @@ -420,6 +456,14 @@ const exampleV3 = `
"type": "string",
"x-formData-name": "fileUpload2",
"x-mimetype": "text/plain"
},
"foo": {
"description": "foo description",
"enum": [
"bar",
"baz"
],
"type": "string"
}
}
},
Expand Down Expand Up @@ -646,6 +690,39 @@ const exampleV3 = `
},
"x-path": "path extension 1",
"x-path2": "path extension 2"
},
"/foo": {
"get": {
"operationId": "getFoo",
"requestBody": {
"x-originalParamName": "foo",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/foo"
}
},
"application/xml": {
"schema": {
"$ref": "#/components/schemas/foo"
}
}
}
},
"responses": {
"default": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/foo"
}
}
},
"description": "OK"
}
},
"summary": "get foo"
}
}
},
"security": [
Expand Down

0 comments on commit fd08c7f

Please sign in to comment.