swagger-api / swagger-api/swagger-codegen

[Go] Incorrectly generated time.Time type for models

Open
#11,790 0 comments 0 reactions 0 assignees View on GitHub

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

swagger.json

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
  1. Execute the swagger-codegen-cli specified above.
  2. 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

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/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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.