OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.58k stars 6.52k forks source link

[BUG] [GO] properties in a multipart/form-data endpoint are not parsed to the correct type in the generated code #16024

Open jacobshedenhelm-wk opened 1 year ago

jacobshedenhelm-wk commented 1 year ago

Bug Report Checklist

Description

Generating an endpoint that accepts a multipart/form-data payload with a non string property in the schema will result in a compilation error in the method.

In the example below, the generated interface method for receiveCake expects a number for the Cake parameter, however, the generated code parses the cake from the form payload as a string and passes the string into the method, thus resulting in a compiler error.

openapi-generator version

openapitools/openapi-generator-cli:v6.6.0 and openapitools/openapi-generator-cli:latest

OpenAPI declaration file content or url

Simplest:

---
info:
  contact:
    email: jacob.shedenhelm@workiva.com
    name: Jacob Shedenhelm
  description: An API for birthday cakes
  title:  Cakes
  version: v0.0.1
openapi: 3.0.3
paths:
  /birthday-cakes/v1/receiveCake:
    post:
      operationId: receiveCake
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                cake:
                 type: number
      responses:
        200:
          description: Ok
      security:
        - {oauth: []}

The issue I am running into is when using a $ref, it also hits the same error.

---
info:
  contact:
    email: jacob.shedenhelm@workiva.com
    name: Jacob Shedenhelm 
  description: An API for birthday cakes 
  title:  Cakes
  version: v0.0.1
openapi: 3.0.3
components:
  schemas:
    Cake:
      type: object
      properties:
        toppings: 
          type: string
        message: 
          type: string
      required:
        - toppings 
        - message 
paths:
  /birthday-cakes/v1/receiveCake:
    post:
      operationId: receiveCake
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                handshake:
                  $ref: '#/components/schemas/Cake'
      responses:
        200:
          description: Ok
      security:
        - {oauth: []}
Generation Details

openapigenerator-cli v6.6.0 & latest Golang

Note - you must set the global-property skipFormModel=true in order to generate the Cake model from the second oas provided, this is an open issue.

Steps to reproduce

use the open api generator to generate code from the top provided oas, navigate to the generated ReceiveCake method, notice compile error.

Related issues/PRs

n/a

wing328 commented 1 year ago

please use --global-property skipFormModel=true for the time being

the current/default behaviour aims to make the output backward-compatible with openapi 2.x

jacobshedenhelm-wk commented 1 year ago

@wing328 Thanks for the response! Unfortunately, setting that property does not fix this issue. That global property you have suggested is needed to have the model actually get generated when using $ref, however, the issue I am reporting is actually in the generated handler for ReceiveCake when it attempts to parse the Cake object from the multipart/form-data payload.