Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2Tov3: handle parameter schema refs #455

Merged
merged 5 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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