Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Allow configuration of yaml minimize quotes #5933

Merged
merged 1 commit into from
Apr 28, 2020
Merged
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 @@ -594,6 +594,7 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> obj
}
}

generateJSONSpecFile(objs);
Copy link
Member

@wing328 wing328 Apr 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jimschubert will line 598 generateYAMLSpecFile(objs); be removed because we're now generating the spec in JSON? or it's intended to generate the spec in both YAML and JSON?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they should both be included for consistency across all generators, tbh. We're limiting what people can extend or customize when we don't have a consistent model bound to templates.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, this is more like a proactive enhancement that the user didn't ask for. I'm ok with that and let's see if there are users asking for something similar in other generators.

generateYAMLSpecFile(objs);

for (Map<String, Object> operations : getOperations(objs)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,38 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import io.swagger.v3.core.util.Yaml;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.core.util.Yaml;
import io.swagger.v3.oas.models.OpenAPI;
import org.openapitools.codegen.config.GlobalSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public static String toYamlString(OpenAPI openAPI) {
if(openAPI == null) {
if (openAPI == null) {
return null;
}
SimpleModule module = createModule();
try {
return Yaml.mapper()
.copy()
.registerModule(module)
ObjectMapper yamlMapper = Yaml.mapper().copy();
// 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
// removing quotes forcibly by default means we are potentially doing a data conversion resulting in an unexpected change to the user's YAML outputs.
// We may allow for property-based enable/disable, retaining the default of enabled for backward compatibility.
if (minimizeYamlQuotes) {
((YAMLFactory) yamlMapper.getFactory()).enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
} else {
((YAMLFactory) yamlMapper.getFactory()).disable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
}
return yamlMapper.registerModule(module)
.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
.writeValueAsString(openAPI)
.replace("\r\n", "\n");
Expand All @@ -34,7 +47,7 @@ public static String toJsonString(OpenAPI openAPI) {
if (openAPI == null) {
return null;
}

SimpleModule module = createModule();
try {
return Json.mapper()
Expand Down