[Go][Client] Multipart array elements gets rendered as struct.String()
Created by: Miroku87
Description
When generating a Go client that has a 'requestBody' of the 'multipart/form-data' type, properties which are arrays of objects gets rendered in the HTTP request body as plain struct.String() values and not with JSON formatted text.
This happens only when the content of the requestBody
is stated under the paths:
property of the YAML.
If the contents are state inside components/requestBodies
the issue does not occur (a different issue occurs but I will make a different issue about that #3045 ).
In the case a property of the form-data
contains an object this object will become an interface and this interface will have to be rendered as a string to be inserted in the localVarFormParams
struct, so the parameterToString
function will be used. Nothing unusual here.
The unusual thing is that, when calling the parameterToString
function, the collectionFormat
parameter is set to csv
causing the program to render the object as if printing it with fmt.Printf('%v',userpayload)
.
openapi-generator version
4.0.1-SNAPSHOT
OpenAPI declaration file content or url
openapi: 3.0.0
servers:
- url: http://petstore.swagger.io/v2
info:
description: 'This is a sample server Petstore server. You can find out more about
Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For
this sample, you can use the api key `special-key` to test the authorization filters.'
version: 1.0.0
title: Swagger Petstore
paths:
"/user/createWithArray":
post:
tags:
- user
summary: Creates list of users with given input array
description: ''
operationId: createUsersWithArrayInput
responses:
default:
description: successful operation
requestBody:
content:
multipart/form-data:
schema:
"$ref": "#/components/schemas/UserPayload"
components:
schemas:
User:
type: object
properties:
email:
type: string
password:
type: string
xml:
name: User
UserPayload:
type: object
properties:
foo:
type: string
bar:
type: array
items:
"$ref": "#/components/schemas/User"
Command line used for generation
I'm using the docker generator:
sudo docker run --rm -v \
${PWD}:/local \
openapitools/openapi-generator-cli generate \
--package-name foo \
-i /local/openapi.yaml \
-g go \
-o /local
Steps to reproduce
Use the above command to generate a client passing the above YAML as input.
Related issues/PRs
Suggest a fix/enhancement
Instead of passing "csv"
as a collectionFormat
parameter to the parameterToString
function the normal thing to do would be to print the object as a JSON string (which the parameterToString
does not yet support as said in this issue: #2964 (closed) )
I think the line in the template is this one: https://github.com/OpenAPITools/openapi-generator/blob/e5a0d183744c3a7d331b77af4d451ff36cc3da16/modules/openapi-generator/src/main/resources/go/api.mustache#L140