Skip to content

Commit af70ab4

Browse files
authored
Merge pull request #1181 from swagger-api/flatten-fix
flatten spec converted from 2.0 when option is true
2 parents 0bdf89a + 6be1793 commit af70ab4

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

modules/swagger-parser-v2-converter/src/main/java/io/swagger/v3/parser/converter/SwaggerConverter.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.swagger.parser.util.SwaggerDeserializationResult;
3434
import io.swagger.v3.core.util.Json;
3535
import io.swagger.v3.core.util.PrimitiveType;
36+
import io.swagger.v3.core.util.Yaml;
3637
import io.swagger.v3.oas.models.Components;
3738
import io.swagger.v3.oas.models.ExternalDocumentation;
3839
import io.swagger.v3.oas.models.OpenAPI;
@@ -60,6 +61,7 @@
6061
import io.swagger.v3.oas.models.security.SecurityScheme;
6162
import io.swagger.v3.oas.models.servers.Server;
6263
import io.swagger.v3.oas.models.tags.Tag;
64+
import io.swagger.v3.parser.OpenAPIV3Parser;
6365
import io.swagger.v3.parser.core.extensions.SwaggerParserExtension;
6466
import io.swagger.v3.parser.core.models.AuthorizationValue;
6567
import io.swagger.v3.parser.core.models.ParseOptions;
@@ -107,7 +109,23 @@ public SwaggerParseResult readContents(String swaggerAsString, List<Authorizatio
107109
result.setSwagger(resolved);
108110
}
109111
}
110-
return convert(result);
112+
SwaggerParseResult out = convert(result);
113+
if (out != null && options != null && options.isFlatten()) {
114+
try {
115+
SwaggerParseResult resultV3 = new OpenAPIV3Parser().readContents(Yaml.pretty(out.getOpenAPI()), auth, options);
116+
out.setOpenAPI(resultV3.getOpenAPI());
117+
if (out.getMessages() != null) {
118+
out.getMessages().addAll(resultV3.getMessages());
119+
out.messages(out.getMessages().stream()
120+
.distinct()
121+
.collect(Collectors.toList()));
122+
} else {
123+
out.messages(resultV3.getMessages());
124+
}
125+
} catch (Exception ignore) {}
126+
}
127+
return out;
128+
111129
}
112130

113131
public List<io.swagger.models.auth.AuthorizationValue> convert(List<AuthorizationValue> auths) {

modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,48 @@ public void test30Url() {
133133
assertEquals(result.getOpenAPI().getOpenapi(), "3.0.1");
134134
}
135135

136+
@Test
137+
public void testConverterWithFlatten() {
138+
String yaml = "swagger: \"2.0\"\n" +
139+
"info:\n" +
140+
" description: \"Foo\"\n" +
141+
" version: \"1.0.0\"\n" +
142+
"host: \"something.com\"\n" +
143+
"basePath: \"/\"\n" +
144+
"schemes:\n" +
145+
" - \"https\"\n" +
146+
"consumes:\n" +
147+
" - \"application/json\"\n" +
148+
"produces:\n" +
149+
" - \"application/json\"\n" +
150+
"paths:\n" +
151+
" /example:\n" +
152+
" get:\n" +
153+
" responses:\n" +
154+
" 200:\n" +
155+
" description: \"OK\"\n" +
156+
" schema:\n" +
157+
" $ref: \"#/definitions/Foo\"\n" +
158+
" parameters: []\n" +
159+
"definitions:\n" +
160+
" Foo:\n" +
161+
" type: \"object\"\n" +
162+
" required:\n" +
163+
" properties:\n" +
164+
" nested:\n" +
165+
" type: \"object\"\n" +
166+
" properties:\n" +
167+
" color:\n" +
168+
" type: \"string\"";
169+
170+
ParseOptions options = new ParseOptions();
171+
options.setResolve(true);
172+
options.setFlatten(true);
173+
SwaggerParseResult result = new OpenAPIParser().readContents(yaml, null, options);
174+
OpenAPI openAPI = result.getOpenAPI();
175+
assertEquals(openAPI.getComponents().getSchemas().size(), 2);
176+
}
177+
136178
@Test
137179
public void test30() {
138180
String json =

0 commit comments

Comments
 (0)