Skip to content

Commit f12fb86

Browse files
authored
[feat] Allow configuration of yaml minimize quotes (#5933)
There are cases where minimizing quotes results in invalid YAML. For example, an input YAML with string "1234_1234" will be converted to YAML value 1234_1234 which is an int in YAML 1.1 (https://yaml.org/type/int.html) THe only option in these cases is to either: * Revert the option completely to always quote values * Provide a user-customization to disable quotes minimization This applies the latter with the assumption that this is an edge case and users who are unaffected will default to the "prettier" version. An alternative would be to write a custom serializer for strings, and if they are in the format of of any of the valid formats defined in YAML: [-+]?0b[0-1_]+ # (base 2) |[-+]?0[0-7_]+ # (base 8) |[-+]?(0|[1-9][0-9_]*) # (base 10) |[-+]?0x[0-9a-fA-F_]+ # (base 16) |[-+]?[1-9][0-9_]*(:[0-5]?[0-9])+ # (base 60) Then wrap the result in quotes. That approach was not taken because of the potential for significant performance impact on very large specs, which our users are often tasked with transforming.
1 parent 6f9d825 commit f12fb86

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> obj
594594
}
595595
}
596596

597+
generateJSONSpecFile(objs);
597598
generateYAMLSpecFile(objs);
598599

599600
for (Map<String, Object> operations : getOperations(objs)) {

modules/openapi-generator/src/main/java/org/openapitools/codegen/serializer/SerializerUtils.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,38 @@
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.MapperFeature;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
56
import com.fasterxml.jackson.databind.module.SimpleModule;
6-
import io.swagger.v3.core.util.Yaml;
7+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
8+
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
79
import io.swagger.v3.core.util.Json;
10+
import io.swagger.v3.core.util.Yaml;
811
import io.swagger.v3.oas.models.OpenAPI;
12+
import org.openapitools.codegen.config.GlobalSettings;
913
import org.slf4j.Logger;
1014
import org.slf4j.LoggerFactory;
1115

1216
public class SerializerUtils {
1317
private static final Logger LOGGER = LoggerFactory.getLogger(SerializerUtils.class);
18+
private static final String YAML_MINIMIZE_QUOTES_PROPERTY = "org.openapitools.codegen.utils.yaml.minimize.quotes";
19+
private static final boolean minimizeYamlQuotes = Boolean.parseBoolean(GlobalSettings.getProperty(YAML_MINIMIZE_QUOTES_PROPERTY, "true"));
1420

1521
public static String toYamlString(OpenAPI openAPI) {
16-
if(openAPI == null) {
22+
if (openAPI == null) {
1723
return null;
1824
}
1925
SimpleModule module = createModule();
2026
try {
21-
return Yaml.mapper()
22-
.copy()
23-
.registerModule(module)
27+
ObjectMapper yamlMapper = Yaml.mapper().copy();
28+
// there is an unfortunate YAML condition where user inputs should be treated as strings (e.g. "1234_1234"), but in yaml this is a valid number and
29+
// removing quotes forcibly by default means we are potentially doing a data conversion resulting in an unexpected change to the user's YAML outputs.
30+
// We may allow for property-based enable/disable, retaining the default of enabled for backward compatibility.
31+
if (minimizeYamlQuotes) {
32+
((YAMLFactory) yamlMapper.getFactory()).enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
33+
} else {
34+
((YAMLFactory) yamlMapper.getFactory()).disable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
35+
}
36+
return yamlMapper.registerModule(module)
2437
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
2538
.writeValueAsString(openAPI)
2639
.replace("\r\n", "\n");
@@ -34,7 +47,7 @@ public static String toJsonString(OpenAPI openAPI) {
3447
if (openAPI == null) {
3548
return null;
3649
}
37-
50+
3851
SimpleModule module = createModule();
3952
try {
4053
return Json.mapper()

0 commit comments

Comments
 (0)