All omitempty fields should be optional

This commit is contained in:
mbohlool 2016-11-20 00:15:12 -08:00
parent 238459b092
commit 71c07650ea

View File

@ -57,9 +57,14 @@ func hasOpenAPITagValue(comments []string, value string) bool {
return false return false
} }
func hasOptionalTag(comments []string) bool { // hasOptionalTag returns true if the member has +optional in its comments or
tagValues := types.ExtractCommentTags("+", comments)[tagOptional] // omitempty in its json tags.
return tagValues != nil func hasOptionalTag(m *types.Member) bool {
hasOptionalCommentTag := types.ExtractCommentTags(
"+", m.CommentLines)[tagOptional] != nil
hasOptionalJsonTag := strings.Contains(
reflect.StructTag(m.Tags).Get("json"), "omitempty")
return hasOptionalCommentTag || hasOptionalJsonTag
} }
// NameSystems returns the name system used by the generators in this package. // NameSystems returns the name system used by the generators in this package.
@ -288,7 +293,7 @@ func (g openAPITypeWriter) generate(t *types.Type) error {
if name == "" { if name == "" {
continue continue
} }
if !hasOptionalTag(m.CommentLines) { if !hasOptionalTag(&m) {
required = append(required, name) required = append(required, name)
} }
if err := g.generateProperty(&m); err != nil { if err := g.generateProperty(&m); err != nil {