OpenAPITools / OpenAPITools/openapi-generator
[BUG][Go] Remove "null" body value when body is empty
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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.
-
The current output is:

-
The expected output would be:

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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the routers.mustache template, specifically the EncodeJSONResponse method named in the report. Generate the go-server output using the supplied OpenAPI specification and config.yaml, then reproduce the POST /users response. Done means an empty 201 response no longer sends a null body.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100