Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.openapitools.codegen.languages;

import com.github.curiousoddman.rgxgen.RgxGen;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.oas.models.examples.Example;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
Expand Down Expand Up @@ -635,18 +636,106 @@ public void setParameterExampleValue(CodegenParameter codegenParameter, Paramete

if (parameter.getExample() != null) {
codegenParameter.example = parameter.getExample().toString();
codegenParameter.vendorExtensions.put("x-py-example", toPythonLiteral(parameter.getExample()));
} else if (parameter.getExamples() != null && !parameter.getExamples().isEmpty()) {
Example example = parameter.getExamples().values().iterator().next();
if (example.getValue() != null) {
codegenParameter.example = example.getValue().toString();
codegenParameter.vendorExtensions.put("x-py-example", toPythonLiteral(example.getValue()));
}
} else if (schema != null && schema.getExample() != null) {
codegenParameter.example = schema.getExample().toString();
codegenParameter.vendorExtensions.put("x-py-example", toPythonLiteral(schema.getExample()));
}

setParameterExampleValue(codegenParameter);
}

protected String toPythonExample(CodegenProperty cp) {
if (cp == null) {
return null;
}

Object example = getSchemaExample(cp.jsonSchema);
if (example != null) {
return toPythonLiteral(example);
}

return null;
}

protected String toPythonExample(CodegenParameter cp) {
if (cp == null) {
return null;
}

Object example = getSchemaExample(cp.jsonSchema);
if (example != null) {
return toPythonLiteral(example);
}

return null;
}

private Object getSchemaExample(String jsonSchema) {
if (StringUtils.isEmpty(jsonSchema)) {
return null;
}
try {
Map<String, Object> schema = Json.mapper().readValue(jsonSchema, Map.class);
Object example = schema.get("example");
if (example != null) {
return example;
}
Object examples = schema.get("examples");
if (examples instanceof List && !((List<?>) examples).isEmpty()) {
return ((List<?>) examples).get(0);
}
return null;
} catch (Exception e) {
return null;
}
}

protected String toPythonLiteral(Object value) {
if (value == null) {
return "None";
}
if (value instanceof String) {
return toPythonStringLiteral((String) value);
}
if (value instanceof Boolean) {
return (Boolean) value ? "True" : "False";
}
if (value instanceof Number) {
return value.toString();
}
if (value instanceof Map) {
List<String> entries = new ArrayList<>();
for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
entries.add(toPythonStringLiteral(String.valueOf(entry.getKey())) + ": " + toPythonLiteral(entry.getValue()));
}
return "{" + StringUtils.join(entries, ", ") + "}";
}
if (value instanceof Iterable) {
List<String> items = new ArrayList<>();
for (Object item : (Iterable<?>) value) {
items.add(toPythonLiteral(item));
}
return "[" + StringUtils.join(items, ", ") + "]";
}

return toPythonStringLiteral(String.valueOf(value));
}

protected String toPythonStringLiteral(String value) {
try {
return Json.mapper().writeValueAsString(value);
} catch (Exception e) {
return "\"" + escapeUnsafeCharacters(value) + "\"";
}
}

@Override
public String sanitizeTag(String tag) {
return sanitizeName(tag);
Expand Down Expand Up @@ -2171,10 +2260,10 @@ private String finalizeType(CodegenProperty cp, PythonType pt) {
pt.annotate("alias", cp.baseName);
}

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

//String defaultValue = null;
if (!cp.required) { //optional
Expand Down Expand Up @@ -2247,11 +2336,6 @@ private String finalizeType(CodegenParameter cp, PythonType pt) {
pt.annotate("description", cp.description);
}

/* TODO support example
if (!StringUtils.isEmpty(cp.getExample())) { // has example
fields.add(String.format(Locale.ROOT, "example=%s", cp.getExample()));
}*/

//return pt.asTypeConstraint(moduleImports);
return pt.asTypeConstraintWithAnnotations(moduleImports);
}
Expand Down
Loading