@@ -141,61 +141,61 @@ public static void setOptionalParameters(
141141 Map <String , Object > parameters ,
142142 boolean isHelper
143143 ) {
144- if (!language .equals ("go" )) {
145- return ;
146- }
144+ if (!"go" .equals (language )) return ;
145+
146+ boolean isBodyRequired = ope .bodyParam != null && ope .bodyParam .required ;
147+ boolean alreadyInlinedBody = ope .allParams .size () == 1 && ope .bodyParam != null && !ope .bodyParam .isArray ;
148+ // I can't figure out the correct condition for this one so it's harcoded for now
149+ boolean isSFFV =
150+ "searchForFacetValues" .equals (ope .operationId ) && !ope .tags .isEmpty () && "composition" .equals (ope .tags .get (0 ).getName ());
151+
147152 int bodyPropsOptional = 0 ;
148- boolean actuallyHasOptional = false ;
149153 boolean isBodyTooBig = false ;
150- boolean isBodyRequired = (ope .bodyParam != null && ope .bodyParam .required );
151- boolean alreadyInlinedBody = ope .allParams .size () == 1 && ope .bodyParam != null && !ope .bodyParam .isArray ;
154+ boolean actuallyHasOptional = false ;
152155
153- if (AlgoliaGoGenerator .canFlattenBody (ope )) {
154- bodyPropsOptional = (int ) ope .bodyParam .getVars ().stream ().filter (prop -> !prop .required ).count ();
155- isBodyTooBig = ope .bodyParam .getVars ().size () == 0 ;
156+ if (AlgoliaGoGenerator .canFlattenBody (ope ) && ope .bodyParam != null ) {
157+ List <CodegenProperty > vars = ope .bodyParam .getVars ();
158+ bodyPropsOptional = (int ) vars .stream ().filter (p -> !p .required ).count ();
159+ isBodyTooBig = vars .isEmpty ();
156160
157- // edge case where the body is already flattened
158- Map <String , Object > paramBody = paramBody = parameters ;
161+ Map <String , Object > paramBody = parameters ;
159162 if (!alreadyInlinedBody ) {
160- Object paramBodyObj = parameters .get (ope .bodyParam .paramName );
161- if (paramBodyObj instanceof String ) {
162- // this is a verbatim paramater, we use it as is
163- System .out .println (ope .operationId + " is a verbatim body " + paramBodyObj );
163+ Object paramObj = parameters .get (ope .bodyParam .paramName );
164+ if (paramObj instanceof String ) {
164165 actuallyHasOptional = !isBodyRequired ;
165- } else {
166- paramBody = (Map <String , Object >) parameters . get ( ope . bodyParam . paramName ) ;
166+ } else if ( paramObj instanceof Map ) {
167+ paramBody = (Map <String , Object >) paramObj ;
167168 }
168169 }
169170
170- for (CodegenProperty prop : ope . bodyParam . getVars () ) {
171+ for (CodegenProperty prop : vars ) {
171172 if (!prop .required && paramBody != null && paramBody .containsKey (prop .baseName )) {
172173 actuallyHasOptional = true ;
173174 }
174175 }
175176 }
176177
177- int totalOptional = ope .optionalParams .size () + bodyPropsOptional ;
178-
179178 for (CodegenParameter param : ope .allParams ) {
180179 if (!param .required && parameters .containsKey (param .baseName )) {
181180 actuallyHasOptional = true ;
182181 break ;
183182 }
184183 }
185184
186- // I can't figure out the correct condition for this one so it's harcoded for now
187- boolean isSFFV = ope .operationId .equals ("searchForFacetValues" ) && "composition" .equals (ope .tags .get (0 ).getName ());
185+ int totalOptional = ope .optionalParams .size () + bodyPropsOptional ;
188186
189187 // hasOptionalWrapper if there is more that one optional param, after the body has been
190- // flattened, only relevant for go
191- test .put ("hasOptionalWrapper" , totalOptional > 1 && actuallyHasOptional && !isSFFV );
192- test .put ("hasInlineOptional" , ((totalOptional == 1 || isSFFV ) && actuallyHasOptional ) || isBodyTooBig );
193- if (isBodyTooBig ) {
194- boolean isBodySet = alreadyInlinedBody ? parameters .size () > 0 : parameters .containsKey (ope .bodyParam .paramName );
195- System .out .println (ope .operationId + " isBodySet: " + isBodySet + " isBodyRequired: " + isBodyRequired );
196- test .put ("hasNilOptional" , isBodyRequired ? totalOptional > 0 && !actuallyHasOptional : !isBodySet );
197- } else {
198- test .put ("hasNilOptional" , totalOptional > 0 && !actuallyHasOptional && !isHelper );
188+ // flattened.
189+ boolean hasOptionalWrapper = totalOptional > 1 && actuallyHasOptional && !isSFFV ;
190+ boolean hasInlineOptional = ((totalOptional == 1 || isSFFV ) && actuallyHasOptional ) || isBodyTooBig ;
191+ boolean hasNilOptional = totalOptional > 0 && !actuallyHasOptional && !isHelper ;
192+ if (isBodyTooBig && !isBodyRequired ) {
193+ boolean isBodySet = alreadyInlinedBody ? !parameters .isEmpty () : parameters .containsKey (ope .bodyParam .paramName );
194+ hasNilOptional = !isBodySet ;
199195 }
196+
197+ test .put ("hasOptionalWrapper" , hasOptionalWrapper );
198+ test .put ("hasInlineOptional" , hasInlineOptional );
199+ test .put ("hasNilOptional" , hasNilOptional );
200200 }
201201}
0 commit comments