|
29 | 29 | import com.fasterxml.jackson.databind.node.JsonNodeFactory;
|
30 | 30 | import com.fasterxml.jackson.databind.node.ObjectNode;
|
31 | 31 | import com.fasterxml.jackson.databind.node.TextNode;
|
| 32 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 33 | +import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; |
32 | 34 | import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
|
| 35 | +import com.fasterxml.jackson.dataformat.yaml.util.StringQuotingChecker; |
33 | 36 | import com.google.common.collect.ImmutableMap;
|
34 | 37 | import com.google.common.collect.Streams;
|
35 | 38 | import io.vavr.control.Try;
|
@@ -137,10 +140,24 @@ protected Map<Path, String> getContentWithSeparateSchemasAsYaml( final Optional<
|
137 | 140 |
|
138 | 141 | protected String jsonToYaml( final JsonNode json ) {
|
139 | 142 | try {
|
140 |
| - return new YAMLMapper().writeValueAsString( json ); |
| 143 | + final YAMLFactory yamlFactory = YAMLFactory.builder() |
| 144 | + .stringQuotingChecker( new OpenApiStringQuotingChecker() ).build(); |
| 145 | + return new YAMLMapper( yamlFactory ).enable( YAMLGenerator.Feature.MINIMIZE_QUOTES ) |
| 146 | + .writeValueAsString( json ); |
141 | 147 | } catch ( final JsonProcessingException exception ) {
|
142 | 148 | LOG.error( "JSON could not be converted to YAML", exception );
|
143 | 149 | return json.toString();
|
144 | 150 | }
|
145 | 151 | }
|
| 152 | + |
| 153 | + private static class OpenApiStringQuotingChecker extends StringQuotingChecker.Default { |
| 154 | + |
| 155 | + @Override |
| 156 | + protected boolean valueHasQuotableChar( final String inputStr ) { |
| 157 | + if ( inputStr.contains( "#" ) ) { |
| 158 | + return true; |
| 159 | + } |
| 160 | + return super.valueHasQuotableChar( inputStr ); |
| 161 | + } |
| 162 | + } |
146 | 163 | }
|
0 commit comments