|
17 | 17 | package org.openapitools.codegen.languages; |
18 | 18 |
|
19 | 19 | import com.github.curiousoddman.rgxgen.RgxGen; |
| 20 | +import io.swagger.v3.core.util.Json; |
20 | 21 | import io.swagger.v3.oas.models.examples.Example; |
21 | 22 | import io.swagger.v3.oas.models.media.Schema; |
22 | 23 | import io.swagger.v3.oas.models.parameters.Parameter; |
@@ -635,18 +636,98 @@ public void setParameterExampleValue(CodegenParameter codegenParameter, Paramete |
635 | 636 |
|
636 | 637 | if (parameter.getExample() != null) { |
637 | 638 | codegenParameter.example = parameter.getExample().toString(); |
| 639 | + codegenParameter.vendorExtensions.put("x-py-example", toPythonLiteral(parameter.getExample())); |
638 | 640 | } else if (parameter.getExamples() != null && !parameter.getExamples().isEmpty()) { |
639 | 641 | Example example = parameter.getExamples().values().iterator().next(); |
640 | 642 | if (example.getValue() != null) { |
641 | 643 | codegenParameter.example = example.getValue().toString(); |
| 644 | + codegenParameter.vendorExtensions.put("x-py-example", toPythonLiteral(example.getValue())); |
642 | 645 | } |
643 | 646 | } else if (schema != null && schema.getExample() != null) { |
644 | 647 | codegenParameter.example = schema.getExample().toString(); |
| 648 | + codegenParameter.vendorExtensions.put("x-py-example", toPythonLiteral(schema.getExample())); |
645 | 649 | } |
646 | 650 |
|
647 | 651 | setParameterExampleValue(codegenParameter); |
648 | 652 | } |
649 | 653 |
|
| 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 | + |
650 | 731 | @Override |
651 | 732 | public String sanitizeTag(String tag) { |
652 | 733 | return sanitizeName(tag); |
@@ -2171,10 +2252,10 @@ private String finalizeType(CodegenProperty cp, PythonType pt) { |
2171 | 2252 | pt.annotate("alias", cp.baseName); |
2172 | 2253 | } |
2173 | 2254 |
|
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 | + } |
2178 | 2259 |
|
2179 | 2260 | //String defaultValue = null; |
2180 | 2261 | if (!cp.required) { //optional |
@@ -2247,11 +2328,6 @@ private String finalizeType(CodegenParameter cp, PythonType pt) { |
2247 | 2328 | pt.annotate("description", cp.description); |
2248 | 2329 | } |
2249 | 2330 |
|
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 | | - |
2255 | 2331 | //return pt.asTypeConstraint(moduleImports); |
2256 | 2332 | return pt.asTypeConstraintWithAnnotations(moduleImports); |
2257 | 2333 | } |
|
0 commit comments