11package com .algolia .codegen ;
22
3+ import com .algolia .codegen .cts .lambda .ScreamingSnakeCaseLambda ;
34import com .algolia .codegen .exceptions .*;
4- import com .algolia .codegen .lambda .ScreamingSnakeCaseLambda ;
55import com .algolia .codegen .utils .*;
6+ import com .fasterxml .jackson .databind .DeserializationFeature ;
7+ import com .fasterxml .jackson .databind .ObjectMapper ;
68import com .google .common .collect .ImmutableMap ;
79import com .google .common .collect .Iterables ;
810import com .samskivert .mustache .Mustache ;
1113import io .swagger .v3 .oas .models .servers .Server ;
1214import java .io .File ;
1315import java .util .*;
16+ import java .util .stream .Collectors ;
1417import org .openapitools .codegen .*;
1518import org .openapitools .codegen .languages .GoClientCodegen ;
1619import org .openapitools .codegen .model .ModelMap ;
1922
2023public class AlgoliaGoGenerator extends GoClientCodegen {
2124
25+ // This is used for the CTS generation
26+ private static final AlgoliaGoGenerator INSTANCE = new AlgoliaGoGenerator ();
27+
2228 @ Override
2329 public String getName () {
2430 return "algolia-go" ;
@@ -29,10 +35,9 @@ public void processOpts() {
2935 String client = (String ) additionalProperties .get ("client" );
3036
3137 additionalProperties .put ("packageName" , client .equals ("query-suggestions" ) ? "suggestions" : Helpers .camelize (client ));
32- additionalProperties .put ("enumClassPrefix" , true );
3338 additionalProperties .put ("is" + Helpers .capitalize (Helpers .camelize (client )) + "Client" , true );
3439
35- String outputFolder = "algolia" + File . separator + client ;
40+ String outputFolder = "algolia/next/" + client ;
3641 setOutputDir (getOutputDir () + File .separator + outputFolder );
3742
3843 super .processOpts ();
@@ -44,6 +49,7 @@ public void processOpts() {
4449 typeMapping .put ("AnyType" , "any" );
4550
4651 modelNameMapping .put ("range" , "modelRange" );
52+ typeMapping .put ("integer" , "int" );
4753
4854 apiTestTemplateFiles .clear ();
4955 modelTestTemplateFiles .clear ();
@@ -54,7 +60,7 @@ public void processOpts() {
5460 supportingFiles .add (new SupportingFile ("configuration.mustache" , "" , "configuration.go" ));
5561 supportingFiles .add (new SupportingFile ("client.mustache" , "" , "client.go" ));
5662
57- Helpers .addCommonSupportingFiles (supportingFiles , "../../" );
63+ Helpers .addCommonSupportingFiles (supportingFiles , "../../../ " );
5864
5965 try {
6066 additionalProperties .put ("packageVersion" , Helpers .getClientConfigField ("go" , "packageVersion" ));
@@ -78,6 +84,13 @@ public void processOpenAPI(OpenAPI openAPI) {
7884 super .processOpenAPI (openAPI );
7985 Helpers .generateServers (super .fromServers (openAPI .getServers ()), additionalProperties );
8086 Timeouts .enrichBundle (openAPI , additionalProperties );
87+ additionalProperties .put (
88+ "appDescription" ,
89+ Arrays .stream (openAPI .getInfo ().getDescription ().split ("\n " ))
90+ .map (line -> "// " + line )
91+ .collect (Collectors .joining ("\n " ))
92+ .trim ()
93+ );
8194 }
8295
8396 @ Override
@@ -118,7 +131,8 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
118131 String modelName = entry .getKey ();
119132 CodegenModel model = entry .getValue ().getModels ().get (0 ).getModel ();
120133
121- // for some reason the property additionalPropertiesIsAnyType is not propagated to the
134+ // for some reason the property additionalPropertiesIsAnyType is not propagated
135+ // to the
122136 // property
123137 for (CodegenProperty prop : model .getVars ()) {
124138 ModelsMap propertyModel = models .get (prop .datatypeWithEnum );
@@ -140,9 +154,105 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
140154 @ Override
141155 public OperationsMap postProcessOperationsWithModels (OperationsMap objs , List <ModelMap > models ) {
142156 OperationsMap operations = super .postProcessOperationsWithModels (objs , models );
157+
158+ // Flatten body params to remove the wrapping object
159+ for (CodegenOperation ope : operations .getOperations ().getOperation ()) {
160+ // clean up the description
161+ String [] lines = ope .unescapedNotes .split ("\n " );
162+ ope .notes = (lines [0 ] +
163+ "\n " +
164+ Arrays .stream (lines )
165+ .skip (1 )
166+ .map (line -> "// " + line )
167+ .collect (Collectors .joining ("\n " ))).trim ();
168+
169+ // enrich the params
170+ for (CodegenParameter param : ope .optionalParams ) {
171+ param .nameInPascalCase = Helpers .capitalize (param .baseName );
172+ }
173+
174+ CodegenParameter bodyParam = ope .bodyParam ;
175+ if (bodyParam != null ) {
176+ flattenBody (ope );
177+ }
178+
179+ // If the optional param struct only has 1 param, we can remove the wrapper
180+ if (ope .optionalParams .size () == 1 ) {
181+ CodegenParameter param = ope .optionalParams .get (0 );
182+
183+ // move it to required, it's easier to handle im mustache
184+ ope .optionalParams .clear ();
185+
186+ ope .requiredParams .add (param );
187+ }
188+ }
189+
143190 ModelPruner .removeOrphanModelFiles (this , operations , models );
144191 Helpers .removeHelpers (operations );
145192 GenericPropagator .propagateGenericsToOperations (operations , models );
146193 return operations ;
147194 }
195+
196+ private void flattenBody (CodegenOperation ope ) {
197+ CodegenParameter bodyParam = ope .bodyParam ;
198+ bodyParam .nameInPascalCase = Helpers .capitalize (bodyParam .baseName );
199+ if (!bodyParam .isModel ) {
200+ return ;
201+ }
202+
203+ if (!canFlattenBody (ope )) {
204+ System .out .println (
205+ "Operation " + ope .operationId + " has body param " + bodyParam .paramName + " in colision with a parameter, skipping flattening"
206+ );
207+ return ;
208+ }
209+
210+ bodyParam .vendorExtensions .put ("x-flat-body" , bodyParam .getVars ().size () > 0 );
211+
212+ if (bodyParam .getVars ().size () > 0 ) {
213+ ope .allParams .removeIf (param -> param .isBodyParam );
214+ ope .requiredParams .removeIf (param -> param .isBodyParam );
215+ ope .optionalParams .removeIf (param -> param .isBodyParam );
216+ }
217+
218+ for (CodegenProperty prop : bodyParam .getVars ()) {
219+ prop .nameInLowerCase = toParamName (prop .baseName );
220+
221+ CodegenParameter param = new ObjectMapper ()
222+ .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
223+ .convertValue (prop , CodegenParameter .class );
224+ param .nameInPascalCase = Helpers .capitalize (prop .baseName );
225+ param .paramName = toParamName (prop .baseName );
226+
227+ if (prop .required ) {
228+ ope .requiredParams .add (param );
229+ } else {
230+ ope .optionalParams .add (param );
231+ }
232+ ope .allParams .add (param );
233+ }
234+ }
235+
236+ public static boolean canFlattenBody (CodegenOperation ope ) {
237+ if (ope .bodyParam == null || !ope .bodyParam .isModel ) {
238+ return false ;
239+ }
240+
241+ if (ope .allParams .size () == 1 ) {
242+ return true ;
243+ }
244+
245+ for (CodegenProperty prop : ope .bodyParam .getVars ()) {
246+ for (CodegenParameter param : ope .allParams ) {
247+ if (param .paramName .equals (prop .baseName )) {
248+ return false ;
249+ }
250+ }
251+ }
252+ return true ;
253+ }
254+
255+ public static String toEnum (String value ) {
256+ return INSTANCE .toEnumVarName (value , "String" );
257+ }
148258}
0 commit comments