Skip to content

Commit 8c8edbe

Browse files
OpenAPIDeserializer: To fix issue #1483, if "examples" and "example" both defined, ignore "example" with a warning
1 parent f344639 commit 8c8edbe

File tree

2 files changed

+163
-4
lines changed

2 files changed

+163
-4
lines changed

modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,12 @@ public MediaType getMediaType(ObjectNode contentNode, String location, ParseResu
10061006

10071007
Object example = getAnyExample("example",contentNode, location,result);
10081008
if (example != null){
1009-
mediaType.setExample(example instanceof NullNode ? null : example);
1009+
if (examplesObject != null) {
1010+
result.warning( location, "examples already defined -- ignoring \"example\" field");
1011+
}
1012+
else {
1013+
mediaType.setExample(example instanceof NullNode ? null : example);
1014+
}
10101015
}
10111016

10121017

@@ -1588,7 +1593,12 @@ public Parameter getParameter(ObjectNode obj, String location, ParseResult resul
15881593

15891594
Object example = getAnyExample("example", obj, location,result);
15901595
if (example != null){
1591-
parameter.setExample(example instanceof NullNode ? null : example);
1596+
if (examplesObject != null) {
1597+
result.warning( location, "examples already defined -- ignoring \"example\" field");
1598+
}
1599+
else {
1600+
parameter.setExample(example instanceof NullNode ? null : example);
1601+
}
15921602
}
15931603

15941604
Boolean allowReserved = getBoolean("allowReserved", obj, false, location, result);
@@ -1708,7 +1718,12 @@ public Header getHeader(ObjectNode headerNode, String location, ParseResult resu
17081718

17091719
Object example = getAnyExample("example", headerNode, location,result);
17101720
if (example != null){
1711-
header.setExample(example instanceof NullNode ? null : example);
1721+
if (examplesObject != null) {
1722+
result.warning( location, "examples already defined -- ignoring \"example\" field");
1723+
}
1724+
else {
1725+
header.setExample(example instanceof NullNode ? null : example);
1726+
}
17121727
}
17131728

17141729
ObjectNode contentNode = getObject("content",headerNode,false,location,result);

modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/util/OpenAPIDeserializerTest.java

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@
6262

6363
import static java.util.Collections.emptyList;
6464
import static org.testng.Assert.assertEquals;
65+
import static org.testng.Assert.assertEqualsNoOrder;
6566
import static org.testng.Assert.assertFalse;
6667
import static org.testng.Assert.assertNotNull;
68+
import static org.testng.Assert.assertNull;
6769
import static org.testng.Assert.assertTrue;
6870

6971
public class OpenAPIDeserializerTest {
@@ -2186,6 +2188,148 @@ public void testSchemaExample(@Injectable List<AuthorizationValue> auths){
21862188
Assert.assertEquals(stateSchemaProperty.getExample(),"CA" );
21872189
}
21882190

2191+
@Test
2192+
public void testExampleVsExamples(){
2193+
String json =
2194+
"{" +
2195+
"\"openapi\": \"3.0.0\"," +
2196+
"\"info\": {\"title\": \"Examples\", \"version\": \"0.0.0\"}," +
2197+
"\"paths\": {}," +
2198+
"\"components\": {" +
2199+
" \"parameters\": {" +
2200+
" \"withExample\": {" +
2201+
" \"name\": \"withExample\"," +
2202+
" \"in\": \"query\"," +
2203+
" \"schema\": {\"type\": \"string\"}," +
2204+
" \"example\": \"Hello\"}," +
2205+
" \"withExamples\": {" +
2206+
" \"name\": \"withExamples\"," +
2207+
" \"in\": \"query\"," +
2208+
" \"schema\": {\"type\": \"string\"}," +
2209+
" \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}," +
2210+
" \"withBoth\": {" +
2211+
" \"name\": \"withBoth\"," +
2212+
" \"in\": \"query\"," +
2213+
" \"schema\": {\"type\": \"string\"}," +
2214+
" \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}," +
2215+
" \"example\": \"Hello\"}," +
2216+
" \"withContentExample\": {" +
2217+
" \"name\": \"withContentExample\"," +
2218+
" \"in\": \"query\"," +
2219+
" \"content\": {" +
2220+
" \"application/json\": {" +
2221+
" \"schema\": {\"type\": \"string\"}," +
2222+
" \"example\": \"Hello\"}}}," +
2223+
" \"withContentExamples\": {" +
2224+
" \"name\": \"withContentExamples\"," +
2225+
" \"in\": \"query\"," +
2226+
" \"content\": {" +
2227+
" \"application/json\": {" +
2228+
" \"schema\": {\"type\": \"string\"}," +
2229+
" \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}," +
2230+
" \"withContentBoth\": {" +
2231+
" \"name\": \"withContentBoth\"," +
2232+
" \"in\": \"query\"," +
2233+
" \"content\": {" +
2234+
" \"application/json\": {" +
2235+
" \"schema\": {\"type\": \"string\"}," +
2236+
" \"example\": \"Hello\"," +
2237+
" \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}}," +
2238+
" \"headers\": {" +
2239+
" \"withExample\": {" +
2240+
" \"schema\": {\"type\": \"string\"}," +
2241+
" \"example\": \"Hello\"}," +
2242+
" \"withExamples\": {" +
2243+
" \"schema\": {\"type\": \"string\"}," +
2244+
" \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}," +
2245+
" \"withBoth\": {" +
2246+
" \"schema\": {\"type\": \"string\"}," +
2247+
" \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}," +
2248+
" \"example\": \"Hello\"}}," +
2249+
" \"requestBodies\": {" +
2250+
" \"withBodyExample\": {" +
2251+
" \"content\": {" +
2252+
" \"application/json\": {" +
2253+
" \"schema\": {\"type\": \"string\"}," +
2254+
" \"example\": \"Hello\"}}}," +
2255+
" \"withBodyExamples\": {" +
2256+
" \"content\": {" +
2257+
" \"application/json\": {" +
2258+
" \"schema\": {\"type\": \"string\"}," +
2259+
" \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}," +
2260+
" \"withBodyBoth\": {" +
2261+
" \"content\": {" +
2262+
" \"application/json\": {" +
2263+
" \"schema\": {\"type\": \"string\"}," +
2264+
" \"example\": \"Hello\"," +
2265+
" \"examples\": {\"Texan\": {\"value\": \"Howdy\"}}}}}}}}";
2266+
2267+
OpenAPIV3Parser parser = new OpenAPIV3Parser();
2268+
SwaggerParseResult result = parser.readContents(json, null, null);
2269+
assertEqualsNoOrder
2270+
(result.getMessages().toArray(),
2271+
new Object[] {
2272+
"attribute components.parameters.withBoth.[withBoth].examples already defined -- ignoring \"example\" field",
2273+
"attribute components.parameters.withContentBoth.[withContentBoth].content.'application/json'.examples already defined -- ignoring \"example\" field",
2274+
"attribute components.requestBodies.withBodyBoth.content.'application/json'.examples already defined -- ignoring \"example\" field",
2275+
"attribute components.headers.withBoth.examples already defined -- ignoring \"example\" field"
2276+
},
2277+
"Expected warnings not found");
2278+
2279+
OpenAPI openAPI = result.getOpenAPI();
2280+
2281+
Parameter param;
2282+
param = openAPI.getComponents().getParameters().get("withExample");
2283+
assertNull( param.getExamples(), "Examples,");
2284+
assertNotNull( param.getExample(), "Example,");
2285+
2286+
param = openAPI.getComponents().getParameters().get("withExamples");
2287+
assertNotNull( param.getExamples(), "Examples,");
2288+
assertNull( param.getExample(), "Example,");
2289+
2290+
param = openAPI.getComponents().getParameters().get("withBoth");
2291+
assertNotNull( param.getExamples(), "Examples,");
2292+
assertNull( param.getExample(), "Example,");
2293+
2294+
Header header;
2295+
header = openAPI.getComponents().getHeaders().get("withExample");
2296+
assertNull( header.getExamples(), "Examples,");
2297+
assertNotNull( header.getExample(), "Example,");
2298+
2299+
header = openAPI.getComponents().getHeaders().get("withExamples");
2300+
assertNotNull( header.getExamples(), "Examples,");
2301+
assertNull( header.getExample(), "Example,");
2302+
2303+
header = openAPI.getComponents().getHeaders().get("withBoth");
2304+
assertNotNull( header.getExamples(), "Examples,");
2305+
assertNull( header.getExample(), "Example,");
2306+
2307+
MediaType mediaType;
2308+
mediaType = openAPI.getComponents().getParameters().get("withContentExample").getContent().get( "application/json");
2309+
assertNull( mediaType.getExamples(), "Examples,");
2310+
assertNotNull( mediaType.getExample(), "Example,");
2311+
2312+
mediaType = openAPI.getComponents().getParameters().get("withContentExamples").getContent().get( "application/json");
2313+
assertNotNull( mediaType.getExamples(), "Examples,");
2314+
assertNull( mediaType.getExample(), "Example,");
2315+
2316+
mediaType = openAPI.getComponents().getParameters().get("withContentBoth").getContent().get( "application/json");
2317+
assertNotNull( mediaType.getExamples(), "Examples,");
2318+
assertNull( mediaType.getExample(), "Example,");
2319+
2320+
mediaType = openAPI.getComponents().getRequestBodies().get("withBodyExample").getContent().get( "application/json");
2321+
assertNull( mediaType.getExamples(), "Examples,");
2322+
assertNotNull( mediaType.getExample(), "Example,");
2323+
2324+
mediaType = openAPI.getComponents().getRequestBodies().get("withBodyExamples").getContent().get( "application/json");
2325+
assertNotNull( mediaType.getExamples(), "Examples,");
2326+
assertNull( mediaType.getExample(), "Example,");
2327+
2328+
mediaType = openAPI.getComponents().getRequestBodies().get("withBodyBoth").getContent().get( "application/json");
2329+
assertNotNull( mediaType.getExamples(), "Examples,");
2330+
assertNull( mediaType.getExample(), "Example,");
2331+
}
2332+
21892333
@Test
21902334
public void testOptionalParameter(@Injectable List<AuthorizationValue> auths) {
21912335
String yaml = "openapi: 3.0.1\n" +
@@ -2465,7 +2609,7 @@ public void readContentObject(JsonNode rootNode) throws Exception {
24652609
Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(0).getContent());
24662610
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().size(),3);
24672611
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getSchema().getType(),"array");
2468-
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getExample(),"example string");
2612+
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getExample(),null);
24692613
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getExamples().get("list").getSummary(),"List of Names");
24702614
Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getSchema().getType(),"array");
24712615

0 commit comments

Comments
 (0)