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

distinguish form data in fromV3RequestBodies #494

Merged
merged 6 commits into from
Feb 23, 2022
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
28 changes: 15 additions & 13 deletions openapi2conv/openapi2_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,27 +698,29 @@ func fromV3RequestBodies(name string, requestBodyRef *openapi3.RequestBodyRef, c
return
}

//Only select one formData or request body for an individual requesstBody as OpenAPI 2 does not support multiples
//Only select one formData or request body for an individual requestBody as OpenAPI 2 does not support multiples
if requestBodyRef.Value != nil {
for contentType, mediaType := range requestBodyRef.Value.Content {
if consumes == nil {
consumes = make(map[string]struct{})
}
consumes[contentType] = struct{}{}
if formParams := FromV3RequestBodyFormData(mediaType); len(formParams) != 0 {
formParameters = formParams
} else {
paramName := name
if originalName, ok := requestBodyRef.Value.Extensions["x-originalParamName"]; ok {
json.Unmarshal(originalName.(json.RawMessage), &paramName)
}
if contentType == "application/x-www-form-urlencoded" || contentType == "multipart/form-data" {
formParameters = FromV3RequestBodyFormData(mediaType)
continue
}

var r *openapi2.Parameter
if r, err = FromV3RequestBody(paramName, requestBodyRef, mediaType, components); err != nil {
return
}
bodyOrRefParameters = append(bodyOrRefParameters, r)
paramName := name
if originalName, ok := requestBodyRef.Value.Extensions["x-originalParamName"]; ok {
json.Unmarshal(originalName.(json.RawMessage), &paramName)
}

var r *openapi2.Parameter
if r, err = FromV3RequestBody(paramName, requestBodyRef, mediaType, components); err != nil {
return
}

bodyOrRefParameters = append(bodyOrRefParameters, r)
}
}
return
Expand Down
91 changes: 91 additions & 0 deletions openapi2conv/openapi2_conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ func TestConvOpenAPIV3ToV2(t *testing.T) {
require.JSONEq(t, exampleV2, string(data))
}

func TestConvOpenAPIV3ToV2WithReqBody(t *testing.T) {
var doc3 openapi3.T
err := json.Unmarshal([]byte(exampleRequestBodyV3), &doc3)
require.NoError(t, err)
{
// Refs need resolving before we can Validate
sl := openapi3.NewLoader()
err = sl.ResolveRefsIn(&doc3, nil)
require.NoError(t, err)
err = doc3.Validate(context.Background())
require.NoError(t, err)
}

doc2, err := FromV3(&doc3)
require.NoError(t, err)
data, err := json.Marshal(doc2)
require.NoError(t, err)
require.JSONEq(t, exampleRequestBodyV2, string(data))
}

func TestConvOpenAPIV2ToV3(t *testing.T) {
var doc2 openapi2.T
err := json.Unmarshal([]byte(exampleV2), &doc2)
Expand Down Expand Up @@ -766,3 +786,74 @@ const exampleV3 = `
"x-root2": "root extension 2"
}
`

const exampleRequestBodyV3 = `{
"info": {
"description": "Test Spec",
"title": "Test Spec",
"version": "0.0.0"
},
"components": {
"requestBodies": {
"FooBody": {
"content": {
"application/json": {
"schema": {
"properties": { "message": { "type": "string" } },
"type": "object"
}
}
},
"description": "test spec request body.",
"required": true
}
}
},
"paths": {
"/foo-path": {
"post": {
"requestBody": { "$ref": "#/components/requestBodies/FooBody" },
"responses": { "202": { "description": "Test spec post." } },
"summary": "Test spec path"
}
}
},
"servers": [{ "url": "http://localhost/" }],
"openapi": "3.0.3"
}
`

const exampleRequestBodyV2 = `{
"basePath": "/",
"consumes": ["application/json"],
"host": "localhost",
"info": {
"description": "Test Spec",
"title": "Test Spec",
"version": "0.0.0"
},
"parameters": {
"FooBody": {
"description": "test spec request body.",
"in": "body",
"name": "FooBody",
"required": true,
"schema": {
"properties": { "message": { "type": "string" } },
"type": "object"
}
}
},
"paths": {
"/foo-path": {
"post": {
"parameters": [{ "$ref": "#/parameters/FooBody" }],
"responses": { "202": { "description": "Test spec post." } },
"summary": "Test spec path"
}
}
},
"schemes": ["http"],
"swagger": "2.0"
}
`