@@ -137,61 +137,61 @@ public static void setOptionalParameters(
137137 Map <String , Object > parameters ,
138138 boolean isHelper
139139 ) {
140- if (!language .equals ("go" )) {
141- return ;
142- }
140+ if (!"go" .equals (language )) return ;
141+
142+ boolean isBodyRequired = ope .bodyParam != null && ope .bodyParam .required ;
143+ boolean alreadyInlinedBody = ope .allParams .size () == 1 && ope .bodyParam != null && !ope .bodyParam .isArray ;
144+ // I can't figure out the correct condition for this one so it's harcoded for now
145+ boolean isSFFV =
146+ "searchForFacetValues" .equals (ope .operationId ) && !ope .tags .isEmpty () && "composition" .equals (ope .tags .get (0 ).getName ());
147+
143148 int bodyPropsOptional = 0 ;
144- boolean actuallyHasOptional = false ;
145149 boolean isBodyTooBig = false ;
146- boolean isBodyRequired = (ope .bodyParam != null && ope .bodyParam .required );
147- boolean alreadyInlinedBody = ope .allParams .size () == 1 && ope .bodyParam != null && !ope .bodyParam .isArray ;
150+ boolean actuallyHasOptional = false ;
148151
149- if (AlgoliaGoGenerator .canFlattenBody (ope )) {
150- bodyPropsOptional = (int ) ope .bodyParam .getVars ().stream ().filter (prop -> !prop .required ).count ();
151- isBodyTooBig = ope .bodyParam .getVars ().size () == 0 ;
152+ if (AlgoliaGoGenerator .canFlattenBody (ope ) && ope .bodyParam != null ) {
153+ List <CodegenProperty > vars = ope .bodyParam .getVars ();
154+ bodyPropsOptional = (int ) vars .stream ().filter (p -> !p .required ).count ();
155+ isBodyTooBig = vars .isEmpty ();
152156
153- // edge case where the body is already flattened
154- Map <String , Object > paramBody = paramBody = parameters ;
157+ Map <String , Object > paramBody = parameters ;
155158 if (!alreadyInlinedBody ) {
156- Object paramBodyObj = parameters .get (ope .bodyParam .paramName );
157- if (paramBodyObj instanceof String ) {
158- // this is a verbatim paramater, we use it as is
159- System .out .println (ope .operationId + " is a verbatim body " + paramBodyObj );
159+ Object paramObj = parameters .get (ope .bodyParam .paramName );
160+ if (paramObj instanceof String ) {
160161 actuallyHasOptional = !isBodyRequired ;
161- } else {
162- paramBody = (Map <String , Object >) parameters . get ( ope . bodyParam . paramName ) ;
162+ } else if ( paramObj instanceof Map ) {
163+ paramBody = (Map <String , Object >) paramObj ;
163164 }
164165 }
165166
166- for (CodegenProperty prop : ope . bodyParam . getVars () ) {
167+ for (CodegenProperty prop : vars ) {
167168 if (!prop .required && paramBody != null && paramBody .containsKey (prop .baseName )) {
168169 actuallyHasOptional = true ;
169170 }
170171 }
171172 }
172173
173- int totalOptional = ope .optionalParams .size () + bodyPropsOptional ;
174-
175174 for (CodegenParameter param : ope .allParams ) {
176175 if (!param .required && parameters .containsKey (param .baseName )) {
177176 actuallyHasOptional = true ;
178177 break ;
179178 }
180179 }
181180
182- // I can't figure out the correct condition for this one so it's harcoded for now
183- boolean isSFFV = ope .operationId .equals ("searchForFacetValues" ) && "composition" .equals (ope .tags .get (0 ).getName ());
181+ int totalOptional = ope .optionalParams .size () + bodyPropsOptional ;
184182
185183 // hasOptionalWrapper if there is more that one optional param, after the body has been
186- // flattened, only relevant for go
187- test .put ("hasOptionalWrapper" , totalOptional > 1 && actuallyHasOptional && !isSFFV );
188- test .put ("hasInlineOptional" , ((totalOptional == 1 || isSFFV ) && actuallyHasOptional ) || isBodyTooBig );
189- if (isBodyTooBig ) {
190- boolean isBodySet = alreadyInlinedBody ? parameters .size () > 0 : parameters .containsKey (ope .bodyParam .paramName );
191- System .out .println (ope .operationId + " isBodySet: " + isBodySet + " isBodyRequired: " + isBodyRequired );
192- test .put ("hasNilOptional" , isBodyRequired ? totalOptional > 0 && !actuallyHasOptional : !isBodySet );
193- } else {
194- test .put ("hasNilOptional" , totalOptional > 0 && !actuallyHasOptional && !isHelper );
184+ // flattened.
185+ boolean hasOptionalWrapper = totalOptional > 1 && actuallyHasOptional && !isSFFV ;
186+ boolean hasInlineOptional = ((totalOptional == 1 || isSFFV ) && actuallyHasOptional ) || isBodyTooBig ;
187+ boolean hasNilOptional = totalOptional > 0 && !actuallyHasOptional && !isHelper ;
188+ if (isBodyTooBig && !isBodyRequired ) {
189+ boolean isBodySet = alreadyInlinedBody ? !parameters .isEmpty () : parameters .containsKey (ope .bodyParam .paramName );
190+ hasNilOptional = !isBodySet ;
195191 }
192+
193+ test .put ("hasOptionalWrapper" , hasOptionalWrapper );
194+ test .put ("hasInlineOptional" , hasInlineOptional );
195+ test .put ("hasNilOptional" , hasNilOptional );
196196 }
197197}
0 commit comments