Skip to content

Commit

Permalink
conversion: fileUpload: handle parameters other than formData
Browse files Browse the repository at this point in the history
This change enables using all possible parameters for file upload.
Previously, only `in: formData` was handled. Now, other parameters
are suported e.g.: `in: path`.
If at least one parameter has `in: formData`, it is considered to
be handled for file upload.
  • Loading branch information
gitforbit committed Jul 17, 2020
1 parent 62affaa commit 5c15267
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 62 deletions.
145 changes: 84 additions & 61 deletions openapi2conv/openapi2_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,49 +130,63 @@ func ToV3PathItem(swagger *openapi2.Swagger, pathItem *openapi2.PathItem) (*open
return result, nil
}

func ToV3RequestBodyFormData(parameters []*openapi2.Parameter) *openapi3.RequestBodyRef {
if len(parameters) == 0 || parameters[0].In != "formData" {
return nil
func ToV3RequestBodyFormData(parameters []*openapi2.Parameter) (*openapi3.RequestBodyRef, openapi3.Parameters) {
if len(parameters) == 0 {
return nil, nil
}
schema := &openapi3.Schema{
Type: "object",
Properties: make(map[string]*openapi3.SchemaRef, len(parameters)),
}
rParameters := openapi3.Parameters{}
//at least one parameter should be `formData`
isFormData := false
for _, parameter := range parameters {
if parameter.In != "formData" || parameter.Name == "" {
if parameter.Name == "" {
continue
}
format := parameter.Format
typ := parameter.Type
if parameter.Type == "file" {
format = "binary"
typ = "string"
if parameter.In == "formData" {
isFormData = true
format := parameter.Format
typ := parameter.Type
if parameter.Type == "file" {
format = "binary"
typ = "string"
}
pschema := &openapi3.Schema{
Description: parameter.Description,
Type: typ,
ExtensionProps: parameter.ExtensionProps,
Format: format,
Enum: parameter.Enum,
Min: parameter.Minimum,
Max: parameter.Maximum,
ExclusiveMin: parameter.ExclusiveMin,
ExclusiveMax: parameter.ExclusiveMax,
MinLength: parameter.MinLength,
MaxLength: parameter.MaxLength,
Default: parameter.Default,
Items: parameter.Items,
MinItems: parameter.MinItems,
MaxItems: parameter.MaxItems,
}
schemaRef := openapi3.SchemaRef{
Value: pschema,
}
schema.Properties[parameter.Name] = &schemaRef
} else {
v3param := toV3ParamaterRef(parameter)
if v3param != nil {
rParameters = append(rParameters, v3param)
}
}
pschema := &openapi3.Schema{
Description: parameter.Description,
Type: typ,
ExtensionProps: parameter.ExtensionProps,
Format: format,
Enum: parameter.Enum,
Min: parameter.Minimum,
Max: parameter.Maximum,
ExclusiveMin: parameter.ExclusiveMin,
ExclusiveMax: parameter.ExclusiveMax,
MinLength: parameter.MinLength,
MaxLength: parameter.MaxLength,
Default: parameter.Default,
Items: parameter.Items,
MinItems: parameter.MinItems,
MaxItems: parameter.MaxItems,
}
schemaRef := openapi3.SchemaRef{
Value: pschema,
}
schema.Properties[parameter.Name] = &schemaRef
}
return &openapi3.RequestBodyRef{
Value: openapi3.NewRequestBody().WithFormDataSchema(schema),
}
if isFormData {
return &openapi3.RequestBodyRef{
Value: openapi3.NewRequestBody().WithFormDataSchema(schema),
}, rParameters
}
return nil, nil
}

func ToV3Operation(swagger *openapi2.Swagger, pathItem *openapi2.PathItem, operation *openapi2.Operation) (*openapi3.Operation, error) {
Expand All @@ -192,9 +206,12 @@ func ToV3Operation(swagger *openapi2.Swagger, pathItem *openapi2.PathItem, opera
result.Security = &resultSecurity
}

requestBodyRef := ToV3RequestBodyFormData(operation.Parameters)
requestBodyRef, parameters := ToV3RequestBodyFormData(operation.Parameters)
if requestBodyRef != nil {
result.RequestBody = requestBodyRef
if len(parameters) > 0 {
result.Parameters = append(result.Parameters, parameters...)
}
} else {
for _, parameter := range operation.Parameters {
v3Parameter, v3RequestBody, err := ToV3Parameter(parameter)
Expand Down Expand Up @@ -222,39 +239,17 @@ func ToV3Operation(swagger *openapi2.Swagger, pathItem *openapi2.PathItem, opera
return result, nil
}

func ToV3Parameter(parameter *openapi2.Parameter) (*openapi3.ParameterRef, *openapi3.RequestBodyRef, error) {
if parameter == nil {
return nil, nil, nil
}
if ref := parameter.Ref; len(ref) > 0 {
return &openapi3.ParameterRef{
Ref: ToV3Ref(ref),
}, nil, nil
}
stripNonCustomExtensions(parameter.Extensions)
in := parameter.In
if in == "body" {
result := &openapi3.RequestBody{
Description: parameter.Description,
Required: parameter.Required,
ExtensionProps: parameter.ExtensionProps,
}
if schemaRef := parameter.Schema; schemaRef != nil {
// Assume it's JSON
result.WithJSONSchemaRef(ToV3SchemaRef(schemaRef))
}
return nil, &openapi3.RequestBodyRef{
Value: result,
}, nil
func toV3ParamaterRef(parameter *openapi2.Parameter) *openapi3.ParameterRef {
if parameter.In == "body" {
return nil
}
result := &openapi3.Parameter{
In: in,
In: parameter.In,
Name: parameter.Name,
Description: parameter.Description,
Required: parameter.Required,
ExtensionProps: parameter.ExtensionProps,
}

if parameter.Type != "" {
schema := &openapi3.SchemaRef{
Value: &openapi3.Schema{
Expand All @@ -277,7 +272,35 @@ func ToV3Parameter(parameter *openapi2.Parameter) (*openapi3.ParameterRef, *open
}
return &openapi3.ParameterRef{
Value: result,
}, nil, nil
}
}

func ToV3Parameter(parameter *openapi2.Parameter) (*openapi3.ParameterRef, *openapi3.RequestBodyRef, error) {
if parameter == nil {
return nil, nil, nil
}
if ref := parameter.Ref; len(ref) > 0 {
return &openapi3.ParameterRef{
Ref: ToV3Ref(ref),
}, nil, nil
}
stripNonCustomExtensions(parameter.Extensions)
in := parameter.In
if in == "body" {
result := &openapi3.RequestBody{
Description: parameter.Description,
Required: parameter.Required,
ExtensionProps: parameter.ExtensionProps,
}
if schemaRef := parameter.Schema; schemaRef != nil {
// Assume it's JSON
result.WithJSONSchemaRef(ToV3SchemaRef(schemaRef))
}
return nil, &openapi3.RequestBodyRef{
Value: result,
}, nil
}
return toV3ParamaterRef(parameter), nil, nil
}

func ToV3Response(response *openapi2.Response) (*openapi3.ResponseRef, error) {
Expand Down
18 changes: 17 additions & 1 deletion openapi2conv/openapi2_conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ const exampleV2 = `
"description": "example post",
"responses": {},
"parameters": [
{
"in": "path",
"name":"id",
"type": "integer",
"description": "File Id"
},
{
"in": "formData",
"name": "fileUpload",
Expand Down Expand Up @@ -460,7 +466,17 @@ const exampleV3 = `
}
}
}
}
},
"parameters": [
{
"in": "path",
"name":"id",
"description": "File Id",
"schema": {
"type": "integer"
}
}
]
},
"put": {
"description": "example put",
Expand Down

0 comments on commit 5c15267

Please sign in to comment.