OpenAPITools / OpenAPITools/openapi-generator

[BUG][GO Server] Syntax error when body is a scalar

Open
#10,786 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • [ X] Have you provided a full/minimal spec to reproduce the issue?
  • [ X] Have you validated the input using an OpenAPI validator (example)?
  • [ X] Have you tested with the latest master to confirm the issue still exists?
  • [X ] Have you searched for related issues/PRs?
  • [ Z] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When the body of a request is a scalar, the initialization uses the type{} syntax which is not applicable to scalar, only structs.
Should use new(type) instead.

openapi-generator version

5.3.0

OpenAPI declaration file content or url
Generation Details

Use the openapi-generator-cli bash script
openapi-generator-cli generate -i spec.yaml -g go-server --enable-post-process-file -o src/ -p featureCORS=true,packageName=test,packageVersion=0.0.1

Generates a function like this:

// SampleBug - sampleBug
func (c *DefaultApiController) SampleBug(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	jobParam := params["job"]
	
	bodyParam := int32{}
	d := json.NewDecoder(r.Body)
	d.DisallowUnknownFields()
	if err := d.Decode(&bodyParam); err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}
...
Steps to reproduce

See above

Related issues/PRs

N/A

Suggest a fix

Should generate something like this for scalars:

// SampleBug - sampleBug
func (c *DefaultApiController) SampleBug(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	jobParam := params["job"]
	
	bodyParam := *new(int32)
	d := json.NewDecoder(r.Body)
	d.DisallowUnknownFields()
	if err := d.Decode(&bodyParam); err != nil {
		c.errorHandler(w, r, &ParsingError{Err: err}, nil)
		return
	}
...

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the provided OpenAPI declaration and the openapi-generator-cli command using the go-server generator. Inspect the generator path responsible for request-body initialization and compare scalar schemas with struct schemas. Done means the generated Go server initializes scalar bodies without a syntax error and handles the shown request successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.