From 2f62fe75b7ec564164c24d50df80b1ff0d60010f Mon Sep 17 00:00:00 2001 From: lsviben Date: Thu, 17 Aug 2023 12:11:12 +0200 Subject: [PATCH] revert adding optional to all required fields --- pkg/types/builder.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pkg/types/builder.go b/pkg/types/builder.go index 961fe02e..c3fa2851 100644 --- a/pkg/types/builder.go +++ b/pkg/types/builder.go @@ -306,13 +306,13 @@ func newTopLevelRequiredParam(path string, includeInit bool) *topLevelRequiredPa } func (r *resource) addParameterField(f *Field, field *types.Var) { - req := !f.Schema.Optional + requiredBySchema := !f.Schema.Optional // Note(turkenh): We are collecting the top level required parameters that // are not identifier fields. This is for generating CEL validation rules for // those parameters and not to require them if the management policy is set // Observe Only. In other words, if we are not creating or managing the // resource, we don't need to provide those parameters which are: - // - req => required + // - requiredBySchema => required // - !f.Identifier => not identifiers - i.e. region, zone, etc. // - len(f.CanonicalPaths) == 1 => top level, i.e. not a nested field // TODO (lsviben): We should add CEL rules for all required fields, @@ -320,17 +320,18 @@ func (r *resource) addParameterField(f *Field, field *types.Var) { // fields now optional. CEL rules should check if a field is // present either in forProvider or initProvider. // https://github.com/upbound/upjet/issues/239 - if req && !f.Identifier && len(f.CanonicalPaths) == 1 { - req = false + if requiredBySchema && !f.Identifier && len(f.CanonicalPaths) == 1 { + requiredBySchema = false // If the field is not a terraform field, we should not require it in init, // as it is not an initProvider field. r.topLevelRequiredParams = append(r.topLevelRequiredParams, newTopLevelRequiredParam(f.TransformedName, f.TFTag != "-")) } - // Note(lsviben): Only fields which are not also initProvider fields should be required. - paramRequired := req && !f.isInit() - f.Comment.Required = pointer.Bool(paramRequired) - if paramRequired { + // Note(lsviben): Only fields which are not also initProvider fields should have a required kubebuilder comment. + f.Comment.Required = pointer.Bool(requiredBySchema && !f.isInit()) + + // For removing omitempty tag from json tag, we are just checking if the field is required by the schema. + if requiredBySchema { // Required fields should not have omitempty tag in json tag. // TODO(muvaf): This overrides user intent if they provided custom // JSON tag.