Skip to content

Commit 8b96546

Browse files
authored
Merge branch 'master' into fix/use-generator-host
2 parents db49210 + 0989603 commit 8b96546

File tree

9 files changed

+65
-16
lines changed

9 files changed

+65
-16
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,6 @@ public interface CodegenConfig {
237237
void setIgnoreImportMapping(boolean ignoreImportMapping);
238238

239239
boolean defaultIgnoreImportMappingOption();
240+
241+
boolean isUsingFlattenSpec();
240242
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4110,4 +4110,8 @@ protected boolean isModelObject(ModelImpl model) {
41104110
}
41114111
return false;
41124112
}
4113+
4114+
public boolean isUsingFlattenSpec() {
4115+
return true;
4116+
}
41134117
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/InlineModelResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* ParseOptions parseOptions = new ParseOptions();
2323
* parseOptions.setFlatten(true);
2424
* Swagger swagger = new SwaggerParser().read(rootNode, new ArrayList<>(), parseOptions);*/
25-
25+
@Deprecated
2626
public class InlineModelResolver {
2727
private Swagger swagger;
2828
private boolean skipMatches;

modules/swagger-codegen/src/main/java/io/swagger/codegen/config/CodegenConfigurator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,9 @@ public ClientOptInput toClientOptInput() {
446446
final List<AuthorizationValue> authorizationValues = AuthParser.parse(auth);
447447
ParseOptions parseOptions = new ParseOptions();
448448
parseOptions.setResolve(true);
449-
parseOptions.setFlatten(true);
449+
if (config.isUsingFlattenSpec()) {
450+
parseOptions.setFlatten(true);
451+
}
450452
Swagger swagger = new SwaggerParser().read(inputSpec, authorizationValues, parseOptions);
451453

452454
input.opts(new ClientOpts())

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SwaggerYamlGenerator.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class SwaggerYamlGenerator extends DefaultCodegen implements CodegenConfi
3232

3333
public static final String OUTPUT_NAME = "outputFile";
3434

35+
public static final String MINMIZE_QUOTES = "minimizeQuotes";
36+
3537
public static final String SWAGGER_FILENAME_DEFAULT_YAML = "swagger.yaml";
3638

3739
protected String outputFile = SWAGGER_FILENAME_DEFAULT_YAML;
@@ -46,6 +48,10 @@ public SwaggerYamlGenerator() {
4648
"output filename")
4749
.defaultValue(SWAGGER_FILENAME_DEFAULT_YAML));
4850

51+
cliOptions.add(new CliOption(MINMIZE_QUOTES,
52+
"minimize quotes")
53+
.defaultValue(Boolean.TRUE.toString()));
54+
4955
supportingFiles.add(new SupportingFile("README.md", "", "README.md"));
5056
}
5157

@@ -80,10 +86,21 @@ public void setOutputFile(String outputFile) {
8086
@Override
8187
public void processSwagger(Swagger swagger) {
8288
try {
83-
final ObjectMapper mapper = new ObjectMapper(new YAMLFactory()
84-
.configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true)
89+
boolean minimizeQuotes = !additionalProperties.containsKey(MINMIZE_QUOTES) ||
90+
(additionalProperties.containsKey(MINMIZE_QUOTES) &&
91+
additionalProperties.get(MINMIZE_QUOTES) instanceof Boolean ?
92+
(Boolean)additionalProperties.get(MINMIZE_QUOTES) :
93+
Boolean.valueOf((String)additionalProperties.get(MINMIZE_QUOTES)
94+
)
95+
);
96+
YAMLFactory yamlFactory = new YAMLFactory()
8597
.configure(YAMLGenerator.Feature.SPLIT_LINES, false)
86-
.configure(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS, true));
98+
.configure(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS, true);
99+
100+
yamlFactory.configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, minimizeQuotes);
101+
102+
final ObjectMapper mapper = new ObjectMapper(yamlFactory);
103+
87104
configureMapper(mapper);
88105
String swaggerString = mapper.writeValueAsString(swagger);
89106
String outputFile = outputFolder + File.separator + this.outputFile;

modules/swagger-codegen/src/test/java/io/swagger/codegen/options/SwaggerYamlOptionsProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class SwaggerYamlOptionsProvider implements OptionsProvider {
1212
public static final String ENSURE_UNIQUE_PARAMS_VALUE = "true";
1313
public static final String OUTPUT_NAME = "swagger.yaml";
1414
public static final String ALLOW_UNICODE_IDENTIFIERS_VALUE = "false";
15+
public static final String MINIMIZE_QUOTES = "true";
1516

1617
@Override
1718
public String getLanguage() {
@@ -25,6 +26,7 @@ public Map<String, String> createOptions() {
2526
.put(CodegenConstants.ENSURE_UNIQUE_PARAMS, ENSURE_UNIQUE_PARAMS_VALUE)
2627
.put(SwaggerYamlGenerator.OUTPUT_NAME, OUTPUT_NAME)
2728
.put(CodegenConstants.ALLOW_UNICODE_IDENTIFIERS, ALLOW_UNICODE_IDENTIFIERS_VALUE)
29+
.put(SwaggerYamlGenerator.MINMIZE_QUOTES, MINIMIZE_QUOTES)
2830
.build();
2931
}
3032

modules/swagger-generator/src/main/java/io/swagger/generator/model/GeneratorInput.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class GeneratorInput {
1414
private SecuritySchemeDefinition auth;
1515
private AuthorizationValue authorizationValue;
1616

17+
private Boolean usingFlattenSpec = true;
18+
1719
public AuthorizationValue getAuthorizationValue() {
1820
return authorizationValue;
1921
}
@@ -57,4 +59,17 @@ public SecuritySchemeDefinition getSecurityDefinition() {
5759
public void setSecurityDefinition(SecuritySchemeDefinition auth) {
5860
this.auth = auth;
5961
}
62+
63+
public Boolean isUsingFlattenSpec() {
64+
return usingFlattenSpec;
65+
}
66+
67+
public Boolean getUsingFlattenSpec() {
68+
return usingFlattenSpec;
69+
}
70+
71+
public void setUsingFlattenSpec(Boolean usingFlattenSpec) {
72+
this.usingFlattenSpec = usingFlattenSpec;
73+
}
74+
6075
}

modules/swagger-generator/src/main/java/io/swagger/generator/online/Generator.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.swagger.models.Swagger;
1111
import io.swagger.models.auth.AuthorizationValue;
1212
import io.swagger.parser.SwaggerParser;
13+
import io.swagger.parser.util.ParseOptions;
1314
import io.swagger.util.Json;
1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
@@ -72,7 +73,19 @@ private static String generate(String language, GeneratorInput opts, Type type)
7273
LOGGER.debug("ignoring empty spec");
7374
node = null;
7475
}
76+
CodegenConfig codegenConfig = null;
77+
try {
78+
codegenConfig = CodegenConfigLoader.forName(language);
79+
} catch (RuntimeException e) {
80+
throw new BadRequestException("Unsupported target " + language + " supplied");
81+
}
82+
7583
Swagger swagger;
84+
ParseOptions parseOptions = new ParseOptions();
85+
parseOptions.setResolve(true);
86+
if (codegenConfig.isUsingFlattenSpec() && !Boolean.FALSE.equals(opts.isUsingFlattenSpec())) {
87+
parseOptions.setFlatten(true);
88+
}
7689
if (node == null) {
7790
if (opts.getSwaggerUrl() != null) {
7891
if (opts.getAuthorizationValue() != null) {
@@ -82,19 +95,19 @@ private static String generate(String language, GeneratorInput opts, Type type)
8295

8396
swagger =
8497
new SwaggerParser().read(opts.getSwaggerUrl(), authorizationValues,
85-
true);
98+
parseOptions);
8699
} else {
87-
swagger = new SwaggerParser().read(opts.getSwaggerUrl());
100+
swagger = new SwaggerParser().read(opts.getSwaggerUrl(), null, parseOptions);
88101
}
89102
} else {
90103
throw new BadRequestException("No swagger specification was supplied");
91104
}
92105
} else if (opts.getAuthorizationValue() != null) {
93106
List<AuthorizationValue> authorizationValues = new ArrayList<AuthorizationValue>();
94107
authorizationValues.add(opts.getAuthorizationValue());
95-
swagger = new SwaggerParser().read(node, authorizationValues, true);
108+
swagger = new SwaggerParser().read(node, authorizationValues, parseOptions);
96109
} else {
97-
swagger = new SwaggerParser().read(node, true);
110+
swagger = new SwaggerParser().read(node, null,parseOptions);
98111
}
99112
if (swagger == null) {
100113
throw new BadRequestException("The swagger specification supplied was not valid");
@@ -116,12 +129,6 @@ private static String generate(String language, GeneratorInput opts, Type type)
116129

117130
clientOptInput.opts(clientOpts).swagger(swagger);
118131

119-
CodegenConfig codegenConfig = null;
120-
try {
121-
codegenConfig = CodegenConfigLoader.forName(language);
122-
} catch (RuntimeException e) {
123-
throw new BadRequestException("Unsupported target " + language + " supplied");
124-
}
125132

126133
if (opts.getOptions() != null) {
127134
codegenConfig.additionalProperties().putAll(opts.getOptions());

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@
952952
</repository>
953953
</repositories>
954954
<properties>
955-
<swagger-parser-version>1.0.59</swagger-parser-version>
955+
<swagger-parser-version>1.0.61-SNAPSHOT</swagger-parser-version>
956956
<scala-version>2.11.1</scala-version>
957957
<felix-version>3.3.0</felix-version>
958958
<swagger-core-version>1.6.6</swagger-core-version>

0 commit comments

Comments
 (0)