From f0e1bdd42557c4694a159df05d5914183d53735a Mon Sep 17 00:00:00 2001 From: Alexander Zielenski <351783+alexzielenski@users.noreply.github.com> Date: Tue, 13 Feb 2024 15:08:50 -0800 Subject: [PATCH] feature: add explicit +required --- pkg/generators/openapi.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/generators/openapi.go b/pkg/generators/openapi.go index dbef0916c..03102b64a 100644 --- a/pkg/generators/openapi.go +++ b/pkg/generators/openapi.go @@ -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. @@ -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 { @@ -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 {