OpenAPITools / OpenAPITools/openapi-generator
[BUG][Go] OpenAPI generator cannot generate required fields with omitempty
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
Hi,
With the openapi-generator Go code generator, it seems it is not possible to generate a Go struct attribute that is a pointer to a type and at the same time contains the omitempty json struct tag when the required OpenAPI attribute is set.
We have the following use case where it seems this would be needed:
We want to define a property that is required and that the user should set and should set it to a non-null value. From our code we need to be able to differentiate between the user not having set the value and having set it to its zero-value.
When digging deeper:
- We want to define an integer attribute as required at OpenAPI level. This integer attribute can have 0 as a valid value.
- In our code we need to be able to differentiate between the user having explicitly set the attribute to 0 and not having set the value at all. Thus, the only option we have is to have to Go autogenerated field as a pointer.
- We don't want to explicitly serialize that field when the pointer value is null, so the omitempty json struct tag is needed.
- The only way to have the omitempty json struct tag seems to be removing the 'required' property at OpenAPI level, but if we do so then at documentation level the attribute is shown as
optionalwhen it shouldn't be optional, we want the user to provide a non-null value always
openapi-generator version
openapi-generator-cli 4.3.1
Thank you
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
Reproduce the Go generator case using an OpenAPI schema with a required integer property, then inspect the generated struct field and JSON tag. Done means the field remains documented as required while generated pointer fields use omitempty so nil values are omitted.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100