OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Go] non-required field of structs incorrectly generated

Open
#9,645 0 comments 6 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
  • 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

There's a quirk in go when serializing the generated model classes.

Take a look at the petstore's Pet schema object, the field category here.

category is not listed as required. So the generated code looks like:

type Pet struct {
	// ...
	Category Category `json:"category,omitempty"`
	// ...
}

So we expect the field to be absent, when it's not explicitly defined.

This does not happen, though. It's always present. The reason is this.

The link summarized: putting omitempty on a field that is a struct and not a base type, will always generate the struct, because a struct doesn't have a default value that could be checked.

This is actually a real bummer, following good API design practices ALL fields should be optional. So this bug now surfaces in our whole API. But only on server-side. E.g. the typescript generation doesn't have this problem.

openapi-generator version

5.1.1

OpenAPI declaration file content or url

petstore example

Generation Details

In the root folder of this project:

docker run -v $(pwd):/workspace docker.io/openapitools/openapi-generator-cli:latest generate -g go-server -i  /workspace/modules/openapi-generator/src/test/resources/3_0/petstore.yaml -o /workspace/tmp
Steps to reproduce

Generate the example as outlined above.

Related issues/PRs

Couldn't find one.

Suggest a fix

Solution: for non-required fields generate a pointer to the struct:

type Pet struct {
	// ...
	Category *Category `json:"category,omitempty"`
	// ...
}

Workaround: define the struct as nullable:

    Category:
      title: Pet category
      description: A category for a pet
      type: object
      nullable: true

However that's not nice at all.
I need to define the nullable on the Category, not where it's actually used (because the Category is $ref'd and not defined inline). So now all usages of category become nullable.

That would be okay...but way more importantly, now the generation for other languages has this nullable + not required twice. E.g. typescript will be generated as category?: Category | null

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

Start with modules/openapi-generator/src/test/resources/3_0/petstore.yaml and generate the go-server output using the Docker command in the issue. Inspect the generated Pet model, especially the non-required category field. Done means non-required struct fields are omitted when undefined without making unrelated nullable changes, and the generated output demonstrates that behavior.

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.