Skip to content

Commit

Permalink
openapi2conv: fix that removes an extra required array from `formDa…
Browse files Browse the repository at this point in the history
…taBody` (#848)
  • Loading branch information
liankui authored Oct 18, 2023
1 parent 62849cf commit 93c2756
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
43 changes: 43 additions & 0 deletions openapi2conv/issue847_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package openapi2conv

import (
"context"
"testing"

"github.com/stretchr/testify/require"
)

func TestIssue847(t *testing.T) {
v2 := []byte(`
swagger: '2.0'
info:
version: '1.10'
title: title
paths:
"/ping":
post:
consumes:
- multipart/form-data
parameters:
- name: file
in: formData
description: file
required: true
type: file
responses:
'200':
description: OK
`)

v3, err := v2v3YAML(v2)
require.NoError(t, err)

err = v3.Validate(context.Background())
require.NoError(t, err)

schemaRequired := v3.Paths["/ping"].Post.RequestBody.Value.Content["multipart/form-data"].Schema.Value.Required
require.Equal(t, schemaRequired, []string{"file"})

fieldRequired := v3.Paths["/ping"].Post.RequestBody.Value.Content["multipart/form-data"].Schema.Value.Properties["file"].Value.Required
require.Nil(t, fieldRequired)
}
8 changes: 7 additions & 1 deletion openapi2conv/openapi2_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ func formDataBody(bodies map[string]*openapi3.SchemaRef, reqs map[string]bool, c
requireds = append(requireds, propName)
}
}
for s, ref := range bodies {
if ref.Value != nil && len(ref.Value.Required) > 0 {
ref.Value.Required = nil
bodies[s] = ref
}
}
schema := &openapi3.Schema{
Type: "object",
Properties: ToV3Schemas(bodies),
Expand Down Expand Up @@ -701,7 +707,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

0 comments on commit 93c2756

Please sign in to comment.