Skip to content

Commit

Permalink
fix: remove formDataBody extra required array
Browse files Browse the repository at this point in the history
  • Loading branch information
liankui committed Oct 17, 2023
1 parent 62849cf commit 8fc11d8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
32 changes: 32 additions & 0 deletions openapi2conv/issue847_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package openapi2conv

import (
"context"
"encoding/json"
"os"
"testing"

"github.com/stretchr/testify/require"

"github.com/getkin/kin-openapi/openapi2"
)

func TestIssue847(t *testing.T) {
doc2file, err := os.Open("testdata/swagger_formdata_required.json")
require.NoError(t, err)
defer doc2file.Close()

var doc2 openapi2.T
err = json.NewDecoder(doc2file).Decode(&doc2)
require.NoError(t, err)

doc3, err := ToV3(&doc2)
require.NoError(t, err)

reqStr, err := json.MarshalIndent(doc3, "", " ")
_ = reqStr
_ = err

err = doc3.Validate(context.Background())
require.NoError(t, err)
}
6 changes: 5 additions & 1 deletion openapi2conv/openapi2_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ func formDataBody(bodies map[string]*openapi3.SchemaRef, reqs map[string]bool, c
requireds = append(requireds, propName)
}
}
for s, ref := range bodies {
ref.Value.Required = nil
bodies[s] = ref
}
schema := &openapi3.Schema{
Type: "object",
Properties: ToV3Schemas(bodies),
Expand Down Expand Up @@ -701,7 +705,7 @@ func fromV3RequestBodies(name string, requestBodyRef *openapi3.RequestBodyRef, c
return
}

//Only select one formData or request body for an individual requestBody 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 {
Expand Down
37 changes: 37 additions & 0 deletions openapi2conv/testdata/swagger_formdata_required.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"swagger": "2.0",
"info": {
"description": "Hello Java Sec API",
"version": "1.10",
"title": "Swagger2 RESTful API"
},
"host": "localhost:8888",
"basePath": "/",
"paths": {
"/UPLOAD/uploadSafe": {
"post": {
"operationId": "singleFileUploadSafeUsingPOST",
"consumes": [
"multipart/form-data"
],
"parameters": [
{
"name": "file",
"in": "formData",
"description": "file",
"required": true,
"type": "file"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
}
}
}
}
}
}

0 comments on commit 8fc11d8

Please sign in to comment.