mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-07-04 06:30:52 +00:00
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:
parent
38ef0e1f3a
commit
2f047fe64a
@ -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;
|
||||
|
@ -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<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
|
||||
|
@ -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':
|
||||
|
Loading…
x
Reference in New Issue
Block a user