Skip to content

Commit 8891f48

Browse files
author
Goopher Maijenburg
committed
Fix FastAPI example generation
1 parent ed72281 commit 8891f48

5 files changed

Lines changed: 2553 additions & 59 deletions

File tree

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

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.openapitools.codegen.languages;
1818

1919
import com.github.curiousoddman.rgxgen.RgxGen;
20+
import io.swagger.v3.core.util.Json;
2021
import io.swagger.v3.oas.models.examples.Example;
2122
import io.swagger.v3.oas.models.media.Schema;
2223
import io.swagger.v3.oas.models.parameters.Parameter;
@@ -635,18 +636,98 @@ public void setParameterExampleValue(CodegenParameter codegenParameter, Paramete
635636

636637
if (parameter.getExample() != null) {
637638
codegenParameter.example = parameter.getExample().toString();
639+
codegenParameter.vendorExtensions.put("x-py-example", toPythonLiteral(parameter.getExample()));
638640
} else if (parameter.getExamples() != null && !parameter.getExamples().isEmpty()) {
639641
Example example = parameter.getExamples().values().iterator().next();
640642
if (example.getValue() != null) {
641643
codegenParameter.example = example.getValue().toString();
644+
codegenParameter.vendorExtensions.put("x-py-example", toPythonLiteral(example.getValue()));
642645
}
643646
} else if (schema != null && schema.getExample() != null) {
644647
codegenParameter.example = schema.getExample().toString();
648+
codegenParameter.vendorExtensions.put("x-py-example", toPythonLiteral(schema.getExample()));
645649
}
646650

647651
setParameterExampleValue(codegenParameter);
648652
}
649653

654+
protected String toPythonExample(CodegenProperty cp) {
655+
if (cp == null) {
656+
return null;
657+
}
658+
659+
Object example = getSchemaExample(cp.jsonSchema);
660+
if (example != null) {
661+
return toPythonLiteral(example);
662+
}
663+
664+
return null;
665+
}
666+
667+
protected String toPythonExample(CodegenParameter cp) {
668+
if (cp == null) {
669+
return null;
670+
}
671+
672+
Object example = getSchemaExample(cp.jsonSchema);
673+
if (example != null) {
674+
return toPythonLiteral(example);
675+
}
676+
677+
return null;
678+
}
679+
680+
private Object getSchemaExample(String jsonSchema) {
681+
if (StringUtils.isEmpty(jsonSchema)) {
682+
return null;
683+
}
684+
try {
685+
Map<String, Object> schema = Json.mapper().readValue(jsonSchema, Map.class);
686+
return schema.get("example");
687+
} catch (Exception e) {
688+
return null;
689+
}
690+
}
691+
692+
protected String toPythonLiteral(Object value) {
693+
if (value == null) {
694+
return "None";
695+
}
696+
if (value instanceof String) {
697+
return toPythonStringLiteral((String) value);
698+
}
699+
if (value instanceof Boolean) {
700+
return (Boolean) value ? "True" : "False";
701+
}
702+
if (value instanceof Number) {
703+
return value.toString();
704+
}
705+
if (value instanceof Map) {
706+
List<String> entries = new ArrayList<>();
707+
for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
708+
entries.add(toPythonStringLiteral(String.valueOf(entry.getKey())) + ": " + toPythonLiteral(entry.getValue()));
709+
}
710+
return "{" + StringUtils.join(entries, ", ") + "}";
711+
}
712+
if (value instanceof Iterable) {
713+
List<String> items = new ArrayList<>();
714+
for (Object item : (Iterable<?>) value) {
715+
items.add(toPythonLiteral(item));
716+
}
717+
return "[" + StringUtils.join(items, ", ") + "]";
718+
}
719+
720+
return toPythonStringLiteral(String.valueOf(value));
721+
}
722+
723+
protected String toPythonStringLiteral(String value) {
724+
try {
725+
return Json.mapper().writeValueAsString(value);
726+
} catch (Exception e) {
727+
return "\"" + escapeUnsafeCharacters(value) + "\"";
728+
}
729+
}
730+
650731
@Override
651732
public String sanitizeTag(String tag) {
652733
return sanitizeName(tag);
@@ -2171,10 +2252,10 @@ private String finalizeType(CodegenProperty cp, PythonType pt) {
21712252
pt.annotate("alias", cp.baseName);
21722253
}
21732254

2174-
/* TODO review as example may break the build
2175-
if (!StringUtils.isEmpty(cp.getExample())) { // has example
2176-
fields.add(String.format(Locale.ROOT, "example=%s", cp.getExample()));
2177-
}*/
2255+
String example = toPythonExample(cp);
2256+
if (example != null) {
2257+
pt.annotate("json_schema_extra", "{\"examples\": [" + example + "]}", false);
2258+
}
21782259

21792260
//String defaultValue = null;
21802261
if (!cp.required) { //optional
@@ -2247,11 +2328,6 @@ private String finalizeType(CodegenParameter cp, PythonType pt) {
22472328
pt.annotate("description", cp.description);
22482329
}
22492330

2250-
/* TODO support example
2251-
if (!StringUtils.isEmpty(cp.getExample())) { // has example
2252-
fields.add(String.format(Locale.ROOT, "example=%s", cp.getExample()));
2253-
}*/
2254-
22552331
//return pt.asTypeConstraint(moduleImports);
22562332
return pt.asTypeConstraintWithAnnotations(moduleImports);
22572333
}

0 commit comments

Comments
 (0)