18
18
package org .openapitools .codegen .kotlin ;
19
19
20
20
import io .swagger .v3 .oas .models .OpenAPI ;
21
- import io .swagger .v3 .oas .models .media .*;
21
+ import io .swagger .v3 .oas .models .media .ArraySchema ;
22
+ import io .swagger .v3 .oas .models .media .DateTimeSchema ;
23
+ import io .swagger .v3 .oas .models .media .IntegerSchema ;
24
+ import io .swagger .v3 .oas .models .media .MapSchema ;
25
+ import io .swagger .v3 .oas .models .media .ObjectSchema ;
26
+ import io .swagger .v3 .oas .models .media .Schema ;
27
+ import io .swagger .v3 .oas .models .media .StringSchema ;
28
+ import org .openapitools .codegen .ClientOptInput ;
29
+ import org .openapitools .codegen .CodegenConstants ;
22
30
import org .openapitools .codegen .CodegenModel ;
23
31
import org .openapitools .codegen .CodegenProperty ;
24
32
import org .openapitools .codegen .DefaultCodegen ;
33
+ import org .openapitools .codegen .DefaultGenerator ;
25
34
import org .openapitools .codegen .TestUtils ;
35
+ import org .openapitools .codegen .config .CodegenConfigurator ;
26
36
import org .openapitools .codegen .languages .KotlinClientCodegen ;
27
37
import org .testng .Assert ;
28
38
import org .testng .annotations .DataProvider ;
29
39
import org .testng .annotations .Test ;
30
40
41
+ import java .io .File ;
42
+ import java .io .IOException ;
43
+ import java .nio .file .Files ;
44
+ import java .nio .file .Paths ;
45
+ import java .util .HashMap ;
46
+ import java .util .List ;
47
+ import java .util .Map ;
48
+
31
49
@ SuppressWarnings ("static-method" )
32
50
public class KotlinClientCodegenModelTest {
33
51
34
52
private Schema getArrayTestSchema () {
35
53
return new ObjectSchema ()
36
- .description ("a sample model" )
37
- .addProperties ("id" , new IntegerSchema ().format ("int64" ))
38
- .addProperties ("examples" , new ArraySchema ().items (new StringSchema ()))
39
- .addRequiredItem ("id" );
54
+ .description ("a sample model" )
55
+ .addProperties ("id" , new IntegerSchema ().format ("int64" ))
56
+ .addProperties ("examples" , new ArraySchema ().items (new StringSchema ()))
57
+ .addRequiredItem ("id" );
40
58
}
41
59
42
60
private Schema getSimpleSchema () {
43
61
return new ObjectSchema ()
44
- .description ("a sample model" )
45
- .addProperties ("id" , new IntegerSchema ().format ("int64" ))
46
- .addProperties ("name" , new StringSchema ())
47
- .addProperties ("createdAt" , new DateTimeSchema ())
48
- .addRequiredItem ("id" )
49
- .addRequiredItem ("name" );
62
+ .description ("a sample model" )
63
+ .addProperties ("id" , new IntegerSchema ().format ("int64" ))
64
+ .addProperties ("name" , new StringSchema ())
65
+ .addProperties ("createdAt" , new DateTimeSchema ())
66
+ .addRequiredItem ("id" )
67
+ .addRequiredItem ("name" );
50
68
}
51
69
52
70
private Schema getMapSchema () {
53
71
return new ObjectSchema ()
54
- .description ("a sample model" )
55
- .addProperties ("mapping" , new MapSchema ()
56
- .additionalProperties (new StringSchema ()));
72
+ .description ("a sample model" )
73
+ .addProperties ("mapping" , new MapSchema ()
74
+ .additionalProperties (new StringSchema ()));
57
75
}
58
76
59
77
private Schema getComplexSchema () {
60
78
return new ObjectSchema ()
61
- .description ("a sample model" )
62
- .addProperties ("child" , new ObjectSchema ().$ref ("#/components/schemas/Child" ));
79
+ .description ("a sample model" )
80
+ .addProperties ("child" , new ObjectSchema ().$ref ("#/components/schemas/Child" ));
63
81
}
64
82
65
83
@ Test (description = "convert a simple model" )
@@ -321,11 +339,11 @@ public void complexPropertyTest() {
321
339
@ DataProvider (name = "modelNames" )
322
340
public static Object [][] modelNames () {
323
341
return new Object [][]{
324
- {"TestNs.TestClass" , new ModelNameTest ("TestNs.TestClass" , "TestNsTestClass" )},
325
- {"$" , new ModelNameTest ("$" , "Dollar" )},
326
- {"for" , new ModelNameTest ("`for`" , "For" )},
327
- {"One<Two" , new ModelNameTest ("One<Two" , "OneLessThanTwo" )},
328
- {"this is a test" , new ModelNameTest ("this is a test" , "ThisIsATest" )}
342
+ {"TestNs.TestClass" , new ModelNameTest ("TestNs.TestClass" , "TestNsTestClass" )},
343
+ {"$" , new ModelNameTest ("$" , "Dollar" )},
344
+ {"for" , new ModelNameTest ("`for`" , "For" )},
345
+ {"One<Two" , new ModelNameTest ("One<Two" , "OneLessThanTwo" )},
346
+ {"this is a test" , new ModelNameTest ("this is a test" , "ThisIsATest" )}
329
347
};
330
348
}
331
349
@@ -341,9 +359,34 @@ public void sanitizeModelNames(final String name, final ModelNameTest testCase)
341
359
Assert .assertEquals (cm .classname , testCase .expectedClassName );
342
360
}
343
361
362
+ @ Test
363
+ public void testNativeClientExplodedQueryParamObject () throws IOException {
364
+ Map <String , Object > properties = new HashMap <>();
365
+ properties .put (CodegenConstants .API_PACKAGE , "xyz.abcdef.api" );
366
+
367
+ File output = Files .createTempDirectory ("test" ).toFile ();
368
+ output .deleteOnExit ();
369
+
370
+ final CodegenConfigurator configurator = new CodegenConfigurator ()
371
+ .setGeneratorName ("kotlin" )
372
+ .setLibrary ("jvm-retrofit2" )
373
+ .setAdditionalProperties (properties )
374
+ .setInputSpec ("src/test/resources/3_0/issue4808.yaml" )
375
+ .setOutputDir (output .getAbsolutePath ().replace ("\\ " , "/" ));
376
+
377
+ final ClientOptInput clientOptInput = configurator .toClientOptInput ();
378
+ DefaultGenerator generator = new DefaultGenerator ();
379
+ List <File > files = generator .opts (clientOptInput ).generate ();
380
+
381
+ Assert .assertEquals (files .size (), 28 );
382
+ TestUtils .assertFileContains (Paths .get (output + "/src/main/kotlin/xyz/abcdef/api/DefaultApi.kt" ),
383
+ "fun getSomeValue(@Query(\" since\" ) since: kotlin.String? = null, @Query(\" sinceBuild\" ) sinceBuild: kotlin.String? = null, @Query(\" maxBuilds\" ) maxBuilds: kotlin.Int? = null, @Query(\" maxWaitSecs\" ) maxWaitSecs: kotlin.Int? = null)"
384
+ );
385
+ }
386
+
344
387
private static class ModelNameTest {
345
- private String expectedName ;
346
- private String expectedClassName ;
388
+ private final String expectedName ;
389
+ private final String expectedClassName ;
347
390
348
391
private ModelNameTest (String nameAndClass ) {
349
392
this .expectedName = nameAndClass ;
0 commit comments