|
62 | 62 |
|
63 | 63 | import static java.util.Collections.emptyList;
|
64 | 64 | import static org.testng.Assert.assertEquals;
|
| 65 | +import static org.testng.Assert.assertEqualsNoOrder; |
65 | 66 | import static org.testng.Assert.assertFalse;
|
66 | 67 | import static org.testng.Assert.assertNotNull;
|
| 68 | +import static org.testng.Assert.assertNull; |
67 | 69 | import static org.testng.Assert.assertTrue;
|
68 | 70 |
|
69 | 71 | public class OpenAPIDeserializerTest {
|
@@ -2186,6 +2188,148 @@ public void testSchemaExample(@Injectable List<AuthorizationValue> auths){
|
2186 | 2188 | Assert.assertEquals(stateSchemaProperty.getExample(),"CA" );
|
2187 | 2189 | }
|
2188 | 2190 |
|
| 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 | + |
2189 | 2333 | @Test
|
2190 | 2334 | public void testOptionalParameter(@Injectable List<AuthorizationValue> auths) {
|
2191 | 2335 | String yaml = "openapi: 3.0.1\n" +
|
@@ -2465,7 +2609,7 @@ public void readContentObject(JsonNode rootNode) throws Exception {
|
2465 | 2609 | Assert.assertNotNull(petByStatusEndpoint.getGet().getParameters().get(0).getContent());
|
2466 | 2610 | Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().size(),3);
|
2467 | 2611 | 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); |
2469 | 2613 | Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getExamples().get("list").getSummary(),"List of Names");
|
2470 | 2614 | Assert.assertEquals(petByStatusEndpoint.getGet().getParameters().get(0).getContent().get("application/json").getSchema().getType(),"array");
|
2471 | 2615 |
|
|
0 commit comments