From 975062aacaca9aa0c212dd98ec0223253ffe8eae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Herv=C3=A9?= Date: Wed, 3 Mar 2021 18:07:34 +0100 Subject: [PATCH 1/3] Fix python example list Closing char was missing. --- .../org/openapitools/codegen/languages/PythonClientCodegen.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java index 505640121dcf..725f6cdcea4f 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java @@ -1087,7 +1087,7 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o } else if (ModelUtils.isArraySchema(schema)) { if (objExample instanceof Iterable) { // If the example is already a list, return it directly instead of wrongly wrap it in another list - return fullPrefix + objExample.toString(); + return fullPrefix + objExample.toString() + closeChars; } ArraySchema arrayschema = (ArraySchema) schema; Schema itemSchema = arrayschema.getItems(); From e92ddefe3ffcafbd1cedad237081c869eae255d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Herv=C3=A9?= Date: Thu, 4 Mar 2021 10:10:56 +0100 Subject: [PATCH 2/3] Add sample, and fix generation for sample not to show up --- .../codegen/languages/PythonClientCodegen.java | 8 ++++---- ...endpoints-models-for-testing-with-http-signature.yaml | 9 +++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java index 725f6cdcea4f..1d51e2a0b7cc 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java @@ -1085,13 +1085,13 @@ private String toExampleValueRecursive(String modelName, Schema schema, Object o } return fullPrefix + example + closeChars; } else if (ModelUtils.isArraySchema(schema)) { - if (objExample instanceof Iterable) { - // If the example is already a list, return it directly instead of wrongly wrap it in another list - return fullPrefix + objExample.toString() + closeChars; - } ArraySchema arrayschema = (ArraySchema) schema; Schema itemSchema = arrayschema.getItems(); String itemModelName = getModelName(itemSchema); + if (objExample instanceof Iterable && itemModelName == null) { + // If the example is already a list, return it directly instead of wrongly wrap it in another list + return fullPrefix + objExample.toString() + closeChars; + } seenSchemas.add(schema); example = fullPrefix + "[" + "\n" + toExampleValueRecursive(itemModelName, itemSchema, objExample, indentationLevel + 1, "", exampleLine + 1, seenSchemas) + ",\n" + closingIndentation + "]" + closeChars; return example; diff --git a/modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml b/modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml index 809235694835..bd29a22a5f9b 100644 --- a/modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml @@ -880,6 +880,15 @@ paths: application/json: schema: $ref: '#/components/schemas/AnimalFarm' + examples: + simple-list: + summary: Simple list example + description: Should not get into code examples + value: + - className: foo + color: yellow + - className: bar + color: green required: false responses: '200': From 5918fc8e04cf40942ba03d9e084d0d4c1eb02797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Herv=C3=A9?= Date: Mon, 8 Mar 2021 12:00:13 +0100 Subject: [PATCH 3/3] Add test of example generation --- .../openapitools/codegen/python/PythonClientTest.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientTest.java index 98f06e7b8894..e133911796b5 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientTest.java @@ -39,7 +39,9 @@ import java.math.BigDecimal; import java.nio.file.Files; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import org.openapitools.codegen.*; import org.openapitools.codegen.languages.PythonClientCodegen; @@ -290,8 +292,9 @@ public void complexMapPropertyTest() { // should not start with 'null'. need help from the community to investigate further @Test(description = "convert an array model") public void arrayModelTest() { - final DefaultCodegen codegen = new PythonClientCodegen(); + final PythonClientCodegen codegen = new PythonClientCodegen(); OpenAPI openAPI = TestUtils.createOpenAPI(); + final Schema model = new ArraySchema() .items(new Schema().$ref("#/components/schemas/Children")) .description("an array model"); @@ -312,6 +315,12 @@ public void arrayModelTest() { Assert.assertEquals(cm.parent, "list"); Assert.assertEquals(cm.imports.size(), 1); Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1); + + final Map childExample = new HashMap<>(); + childExample.put("number", 3); + final List> example = Arrays.asList(childExample); + String exampleValue = codegen.toExampleValue(model, example); + Assert.assertEquals("[Children(number=1,),]", exampleValue.replaceAll("\\s+","")); } // should not start with 'null'. need help from the community to investigate further