Skip to content

Commit dedc38c

Browse files
committed
Address reviewer feedback: check plural examples in getSchemaExample
- Fall back to first entry of `examples` array when `example` is absent, fixing schema example extraction for OAS 3.1 specs. - Add unit tests for plural and singular example precedence via TestableFastAPICodegen helper subclass. - Add nickname property with examples array to test fixture.
1 parent 8f0e3e1 commit dedc38c

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,15 @@ private Object getSchemaExample(String jsonSchema) {
683683
}
684684
try {
685685
Map<String, Object> schema = Json.mapper().readValue(jsonSchema, Map.class);
686-
return schema.get("example");
686+
Object example = schema.get("example");
687+
if (example != null) {
688+
return example;
689+
}
690+
Object examples = schema.get("examples");
691+
if (examples instanceof List && !((List<?>) examples).isEmpty()) {
692+
return ((List<?>) examples).get(0);
693+
}
694+
return null;
687695
} catch (Exception e) {
688696
return null;
689697
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/python/PythonFastAPIServerCodegenTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.swagger.v3.oas.models.OpenAPI;
55
import io.swagger.v3.parser.core.models.ParseOptions;
66
import org.openapitools.codegen.ClientOptInput;
7+
import org.openapitools.codegen.CodegenProperty;
78
import org.openapitools.codegen.DefaultCodegen;
89
import org.openapitools.codegen.DefaultGenerator;
910
import org.openapitools.codegen.languages.PythonFastAPIServerCodegen;
@@ -24,6 +25,13 @@
2425

2526
public class PythonFastAPIServerCodegenTest {
2627

28+
/** Exposes protected toPythonExample for unit testing. */
29+
private static class TestableFastAPICodegen extends PythonFastAPIServerCodegen {
30+
public String exposeToPythonExample(CodegenProperty cp) {
31+
return toPythonExample(cp);
32+
}
33+
}
34+
2735
// Helper function, intended to reduce boilerplate
2836
static private String generateFiles(DefaultCodegen codegen, String filePath) throws IOException {
2937
final File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
@@ -77,4 +85,24 @@ public void testSchemaPropertyExamplesInMetadata() throws IOException {
7785
assertFileContains(model, "name: StrictStr = Field(json_schema_extra={\"examples\": [\"doggie\"]})");
7886
assertFileNotContains(model, "json_schema_extra={\"examples\": [\"''\"]}");
7987
}
88+
89+
@Test(description = "toPythonExample picks first entry from plural examples array in jsonSchema")
90+
public void testToPythonExampleWithPluralExamples() {
91+
final TestableFastAPICodegen codegen = new TestableFastAPICodegen();
92+
CodegenProperty cp = new CodegenProperty();
93+
cp.name = "nickname";
94+
cp.jsonSchema = "{\"type\": \"string\", \"examples\": [\"buddy\", \"pal\"]}";
95+
96+
Assert.assertEquals(codegen.exposeToPythonExample(cp), "\"buddy\"");
97+
}
98+
99+
@Test(description = "toPythonExample prefers singular example over plural examples in jsonSchema")
100+
public void testToPythonExamplePrefersExampleOverExamples() {
101+
final TestableFastAPICodegen codegen = new TestableFastAPICodegen();
102+
CodegenProperty cp = new CodegenProperty();
103+
cp.name = "nickname";
104+
cp.jsonSchema = "{\"type\": \"string\", \"example\": \"doggie\", \"examples\": [\"buddy\", \"pal\"]}";
105+
106+
Assert.assertEquals(codegen.exposeToPythonExample(cp), "\"doggie\"");
107+
}
80108
}

modules/openapi-generator/src/test/resources/3_0/python-fastapi/petstore-with-examples.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,12 @@ components:
14061406
name:
14071407
type: string
14081408
example: doggie
1409+
nickname:
1410+
type: string
1411+
description: Optional nickname for the pet
1412+
examples:
1413+
- buddy
1414+
- pal
14091415
photoUrls:
14101416
type: array
14111417
xml:

0 commit comments

Comments
 (0)