Skip to content

Commit

Permalink
V3: Remove omitempty on nullable complex types (#628)
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com>
  • Loading branch information
pierre-emmanuelJ authored Apr 30, 2024
1 parent f632437 commit 006c2df
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
Unreleased
----------

- v3: Remove omitempty on nullable complex types #628
- v3: Add the Authorization header value in the dump request #624
- v3: Add the User-Agent header value #623
- go.mk: remove submodule and initialize through make #618
Expand Down
13 changes: 6 additions & 7 deletions v3/generator/schemas/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,12 @@ func renderObject(typeName string, s *base.Schema, output *bytes.Buffer) (string
tag := fmt.Sprintf(" `json:\"%s,omitempty\"", propName)

pointer := "*"
req := isRequiredField(propName, s)
if req {
required := isRequiredField(propName, s)
// Do not remove omitempty on simple nullable types.
if required || (nullable && !IsSimpleSchema(prop)) {
tag = fmt.Sprintf(" `json:%q", propName)
}
validation := renderValidation(prop, req)
validation := renderValidation(prop, required)
if validation != "" {
tag += " " + validation
}
Expand Down Expand Up @@ -485,15 +486,13 @@ func InferType(s *base.Schema) {
}
}

// IsSimpleSchema returns whether this schema is a scalar or array as these
// can't be circular references. Objects result in `false` and that triggers
// circular ref checks.
// IsSimpleSchema returns true if the schema is a scalar type, false otherwise.
func IsSimpleSchema(s *base.Schema) bool {
if len(s.Type) == 0 {
return true
}

return s.Type[0] != "object" && s.Type[0] != "map"
return s.Type[0] != "object" && s.Type[0] != "map" && s.Type[0] != "array"
}

func renderDoc(s *base.Schema) string {
Expand Down
10 changes: 5 additions & 5 deletions v3/operations.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions v3/schemas.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 006c2df

Please sign in to comment.