Skip to content

Commit

Permalink
Merge pull request #448 from alexzielenski/required
Browse files Browse the repository at this point in the history
feature: add explicit +required
  • Loading branch information
k8s-ci-robot authored Feb 21, 2024
2 parents 30be70b + f0e1bdd commit 2ac9dc5
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pkg/generators/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
const tagName = "k8s:openapi-gen"
const markerPrefix = "+k8s:validation:"
const tagOptional = "optional"
const tagRequired = "required"
const tagDefault = "default"

// Known values for the tag.
Expand Down Expand Up @@ -80,6 +81,11 @@ func hasOpenAPITagValue(comments []string, value string) bool {
return false
}

func hasRequiredTag(m *types.Member) bool {
return types.ExtractCommentTags(
"+", m.CommentLines)[tagRequired] != nil
}

// hasOptionalTag returns true if the member has +optional in its comments or
// omitempty in its json tags.
func hasOptionalTag(m *types.Member) bool {
Expand Down Expand Up @@ -317,7 +323,10 @@ func (g openAPITypeWriter) generateMembers(t *types.Type, required []string) ([]
if name == "" {
continue
}
if !hasOptionalTag(&m) {
if isOptional, isRequired := hasOptionalTag(&m), hasRequiredTag(&m); isOptional && isRequired {
klog.Errorf("Error when generating: %v, %v\n", name, m)
return required, fmt.Errorf("member %s of type %s cannot be both optional and required", m.Name, t.Name)
} else if !isOptional || isRequired {
required = append(required, name)
}
if err = g.generateProperty(&m, t); err != nil {
Expand Down

0 comments on commit 2ac9dc5

Please sign in to comment.