Skip to content
This repository was archived by the owner on Dec 25, 2024. It is now read-only.

Commit d48209e

Browse files
Kevin-BannierKevin Bannier
andauthored
fix: incorrect boolean literal in unit tests [python-flask] (#13397)
Co-authored-by: Kevin Bannier <kevin.bannier@outlook.fr>
1 parent b9d7158 commit d48209e

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717
package 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;
1925
import com.google.common.collect.ArrayListMultimap;
2026
import com.google.common.collect.Lists;
2127
import com.google.common.collect.Multimap;
@@ -42,11 +48,19 @@
4248
import org.slf4j.LoggerFactory;
4349

4450
import java.io.File;
51+
import java.io.IOException;
4552
import java.util.*;
4653

4754
import static org.openapitools.codegen.utils.StringUtils.camelize;
4855

4956
public 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

Comments
 (0)