1616
1717package org .openapitools .codegen .languages ;
1818
19+ import com .fasterxml .jackson .core .JsonGenerator ;
20+ import com .fasterxml .jackson .core .type .TypeReference ;
21+ import com .fasterxml .jackson .databind .ObjectMapper ;
22+ import com .fasterxml .jackson .databind .JsonSerializer ;
23+ import com .fasterxml .jackson .databind .SerializerProvider ;
24+ import com .fasterxml .jackson .databind .module .SimpleModule ;
1925import com .google .common .collect .ArrayListMultimap ;
2026import com .google .common .collect .Lists ;
2127import com .google .common .collect .Multimap ;
4248import org .slf4j .LoggerFactory ;
4349
4450import java .io .File ;
51+ import java .io .IOException ;
4552import java .util .*;
4653
4754import static org .openapitools .codegen .utils .StringUtils .camelize ;
4855
4956public abstract class AbstractPythonConnexionServerCodegen extends AbstractPythonCodegen implements CodegenConfig {
57+ private static class PythonBooleanSerializer extends JsonSerializer <Boolean > {
58+ @ Override
59+ public void serialize (Boolean value , JsonGenerator gen , SerializerProvider serializers ) throws IOException {
60+ gen .writeRawValue (value ? "True" : "False" );
61+ }
62+ }
63+
5064 private final Logger LOGGER = LoggerFactory .getLogger (AbstractPythonConnexionServerCodegen .class );
5165
5266 public static final String CONTROLLER_PACKAGE = "controllerPackage" ;
@@ -60,6 +74,10 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
6074 public static final String MOVE_TESTS_UNDER_PYTHON_SRC_ROOT = "testsUsePythonSrcRoot" ;
6175 static final String MEDIA_TYPE = "mediaType" ;
6276
77+ // An object mapper that is used to convert an example string to
78+ // a "python-compliant" example string (boolean as True/False).
79+ final ObjectMapper MAPPER = new ObjectMapper ();
80+
6381 protected int serverPort = 8080 ;
6482 protected String controllerPackage ;
6583 protected String defaultController ;
@@ -80,6 +98,10 @@ public AbstractPythonConnexionServerCodegen(String templateDirectory, boolean fi
8098 modelPackage = "models" ;
8199 testPackage = "test" ;
82100
101+ SimpleModule simpleModule = new SimpleModule ();
102+ simpleModule .addSerializer (Boolean .class , new PythonBooleanSerializer ());
103+ MAPPER .registerModule (simpleModule );
104+
83105 // TODO may remove these later to default to the setting in abstract python base class instead
84106 languageSpecificPrimitives .add ("List" );
85107 languageSpecificPrimitives .add ("Dict" );
@@ -695,7 +717,16 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
695717 if (operation .requestBodyExamples != null ) {
696718 for (Map <String , String > example : operation .requestBodyExamples ) {
697719 if (example .get ("contentType" ) != null && example .get ("contentType" ).equals ("application/json" )) {
698- operation .bodyParam .example = example .get ("example" );
720+ // Make an example dictionary more python-like (Booleans True/False).
721+ // If fails, use the original string.
722+ try {
723+ Map <String , Object > result = MAPPER .readValue (example .get ("example" ),
724+ new TypeReference <Map <String , Object >>() {
725+ });
726+ operation .bodyParam .example = MAPPER .writeValueAsString (result );
727+ } catch (IOException e ) {
728+ operation .bodyParam .example = example .get ("example" );
729+ }
699730 }
700731 }
701732 }
0 commit comments