[BUG][Go] Remove "null" body value when body is empty
Created by: rcebrian
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
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
- Configuration file (
config.yaml
):
outputAsLibrary: true
addResponseHeaders: true
sourceFolder: openapi
packageName: server
serverPort: 8080
SPECS_FILE="api/openapi-spec/openapi.yaml"
CONFIG_FILE="api/openapi-spec/config.yaml"
OUTPUT_DIR="internal/platform/server"
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" }
- Method:
Related issues/PRs
.
Suggest a fix
Edit routers.go
template, on method EncodeJSONResponse
encode body if not null, nothing otherwise