Fix python example list (#8877)

* Fix python example list

Closing char was missing.

* Add sample, and fix generation for sample not to show up

* Add test of example generation
This commit is contained in:
Thomas Hervé 2021-03-10 17:27:48 +01:00 committed by GitHub
parent 38ef0e1f3a
commit 2f047fe64a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 5 deletions

View File

@ -1085,13 +1085,13 @@ public class PythonClientCodegen extends PythonLegacyClientCodegen {
} }
return fullPrefix + example + closeChars; return fullPrefix + example + closeChars;
} else if (ModelUtils.isArraySchema(schema)) { } 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; ArraySchema arrayschema = (ArraySchema) schema;
Schema itemSchema = arrayschema.getItems(); Schema itemSchema = arrayschema.getItems();
String itemModelName = getModelName(itemSchema); 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); seenSchemas.add(schema);
example = fullPrefix + "[" + "\n" + toExampleValueRecursive(itemModelName, itemSchema, objExample, indentationLevel + 1, "", exampleLine + 1, seenSchemas) + ",\n" + closingIndentation + "]" + closeChars; example = fullPrefix + "[" + "\n" + toExampleValueRecursive(itemModelName, itemSchema, objExample, indentationLevel + 1, "", exampleLine + 1, seenSchemas) + ",\n" + closingIndentation + "]" + closeChars;
return example; return example;

View File

@ -39,7 +39,9 @@ import java.io.File;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import org.openapitools.codegen.*; import org.openapitools.codegen.*;
import org.openapitools.codegen.languages.PythonClientCodegen; 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 // should not start with 'null'. need help from the community to investigate further
@Test(description = "convert an array model") @Test(description = "convert an array model")
public void arrayModelTest() { public void arrayModelTest() {
final DefaultCodegen codegen = new PythonClientCodegen(); final PythonClientCodegen codegen = new PythonClientCodegen();
OpenAPI openAPI = TestUtils.createOpenAPI(); OpenAPI openAPI = TestUtils.createOpenAPI();
final Schema model = new ArraySchema() final Schema model = new ArraySchema()
.items(new Schema().$ref("#/components/schemas/Children")) .items(new Schema().$ref("#/components/schemas/Children"))
.description("an array model"); .description("an array model");
@ -312,6 +315,12 @@ public class PythonClientTest {
Assert.assertEquals(cm.parent, "list"); Assert.assertEquals(cm.parent, "list");
Assert.assertEquals(cm.imports.size(), 1); Assert.assertEquals(cm.imports.size(), 1);
Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1); Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("Children")).size(), 1);
final Map<String, Integer> childExample = new HashMap<>();
childExample.put("number", 3);
final List<Map<String, Integer>> 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 // should not start with 'null'. need help from the community to investigate further

View File

@ -880,6 +880,15 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/AnimalFarm' $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 required: false
responses: responses:
'200': '200':