swagger-api / swagger-api/swagger-codegen
[Go] Incorrectly generated time.Time type for models
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
Go client/server generator excludes * for nullable/optional time.Time types. Because time.Time is a struct it will never get omitted from the models meaning that even if the time paramater is not set it will be sent with default values. omitempty works with primitives and pointers only.
Swagger-codegen version
swagger-codegen-cli-2.4.27.jar
Swagger declaration file content or url
Command line used for generation
java -jar swagger-codegen-cli-2.4.27.jar generate -i pet.json -l go -o go-client
Steps to reproduce
- Execute the swagger-codegen-cli specified above.
- Check the generated
model_pet_dto.go
Related issues/PRs
#11791
Suggest a fix/enhancement
This and this templates need to be fixed.
Change from:
{{name}} {{^isEnum}}{{^isPrimitiveType}}{{^isContainer}}{{^isDateTime}}*{{/isDateTime}}{{/isContainer}}{{/isPrimitiveType}}{{/isEnum}}{{{datatype}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{baseName}}"{{/withXml}}`
to:
{{name}} {{^isEnum}}{{^isPrimitiveType}}{{^isContainer}}*{{/isContainer}}{{/isPrimitiveType}}{{/isEnum}}{{{datatype}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{baseName}}"{{/withXml}}`
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 modules/swagger-codegen/src/main/resources/go/model.mustache and go-server/model.mustache, then reproduce the issue using swagger-codegen-cli-2.4.27.jar and the linked swagger.json. Inspect the generated model_pet_dto.go and verify that optional time.Time fields are represented and serialized correctly when unset.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 50/100