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 505640121dc..1d51e2a0b7c 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 @@ public class PythonClientCodegen extends PythonLegacyClientCodegen { } 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(); - } 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/java/org/openapitools/codegen/python/PythonClientTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonClientTest.java index 98f06e7b889..e133911796b 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.io.File; 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 class PythonClientTest { // 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 class PythonClientTest { 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 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 80923569483..bd29a22a5f9 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':