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.85k stars 6.59k forks source link

[BUG][Go] Remove "null" body value when body is empty #13927

Open rcebrian opened 2 years ago

rcebrian commented 2 years ago

Bug Report Checklist

Description

If you want to close the response with no body, the generated code sends null as body. For example, you need to create a new user with name and firstname, if everything is ok, the user is created and the api wants to return 201 - Created with no body.

openapi-generator version

Tool: @openapitools/openapi-generator-cli@2.5.2

openapi-generator-cli 6.2.1
  commit : b0ce532 
  built  : 2022-11-01
  source : https://github.com/openapitools/openapi-generator
  docs   : https://openapi-generator.tech/
OpenAPI declaration file content or url
openapi: 3.0.3
info:
  description: Users API
  version: 1.0.0
  title: Basic API
  contact:
    email: rcebrian@github.com
  license:
    name: Apache 2.0
    url: 'https://www.apache.org/licenses/LICENSE-2.0.html'
servers:
  - url: 'http://localhost:8080'
    description: localhost
tags:
  - name: users
    description: Users use cases
paths:
  /users:
    post:
      summary: Create user
      operationId: createUser
      responses:
        '201':
          description: Created
        '400':
          description: Bad Request
          content:
            application/json:
              schema:
                type: object
                properties:
                  errors:
                    type: string
        '500':
          description: Internal Server Error
      description: Save user into data storage
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/User'
      tags:
        - users
components:
  schemas:
    User:
      title: User
      type: object
      description: ''
      properties:
        id:
          type: string
        name:
          type: string
        firstname:
          type: string
      required:
        - name
        - firstname
  responses: {}
Generation Details

export GO_POST_PROCESS_FILE="goimports -w" openapi-generator-cli generate --generator-name go-server \ --input-spec $SPECS_FILE \ --config $CONFIG_FILE \ --global-property apiDocs=true \ --global-property verbose=false \ --model-name-suffix dto \ --enable-post-process-file \ -o $OUTPUT_DIR



##### Steps to reproduce

* Start the server on port ```8080```
* MAke request
  * Method: ```POST```
  * ```localhost:8080/users```
  * Body: ```{
    "name": "John",
    "firstname": "Doe"
}```

##### Related issues/PRs

.

##### Suggest a fix
Edit ```routers.go``` [template](https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/go-server/routers.mustache), on method ```EncodeJSONResponse``` encode body if not null, nothing otherwise
wing328 commented 2 years ago

@rcebrian thanks for reporting the issue. I wonder if you can file a PR with the suggested fix and we'll review accordingly.

rcebrian commented 2 years ago

Hi @wing328, you can see the suggested fix on this PR: https://github.com/OpenAPITools/openapi-generator/pull/13934