3232import java .util .Map ;
3333import java .util .function .Function ;
3434
35+ import static org .apache .commons .lang3 .StringUtils .substringAfterLast ;
3536import static org .hamcrest .CoreMatchers .is ;
3637import static org .hamcrest .CoreMatchers .notNullValue ;
3738import static org .hamcrest .MatcherAssert .assertThat ;
@@ -120,7 +121,7 @@ void testExamplesProcessor() throws OpenAPILoaderException, URISyntaxException {
120121 String spec = System .getProperty ("spec" , inputUri );
121122 OpenAPI openAPI = OpenAPILoader .load (spec );
122123 OpenAPI openAPIUnproccessed = openAPI ;
123- new ExamplesProcessor (openAPI ,spec ).processExamples (openAPI );
124+ new ExamplesProcessor (openAPI ,spec ).processExamples ();
124125 assertEquals (openAPIUnproccessed , openAPI );
125126 }
126127
@@ -131,7 +132,7 @@ void testExamplesProcessorComponentError() throws OpenAPILoaderException {
131132 OpenAPI openAPI = OpenAPILoader .load (spec );
132133 ExamplesProcessor examplesProcessor = new ExamplesProcessor (openAPI ,spec );
133134 try {
134- examplesProcessor .processExamples (openAPI );
135+ examplesProcessor .processExamples ();
135136 fail ("Expected TransformerException" );
136137 }catch (TransformerException e ){
137138 assertEquals ("Failed to process example content for ExampleHolder{name='example-in-components', ref=null}" ,e .getMessage ());
@@ -166,15 +167,13 @@ void testBundleApi() throws OpenAPILoaderException, IOException {
166167 openAPI .getComponents ().getExamples ().get ("example-in-components-1" ).getSummary (),
167168 is ("component-examples with example - should be left alone" ));
168169
169-
170170 // actual input is not valid... when there is a ref no other properties should be set - still allow it.
171171 assertThat ("Component example that duplicates a inline example, is left alone. But the summary is removed" ,
172172 openAPI .getComponents ().getExamples ().get ("example-number-one" ).getSummary (), is ("example-number-one" ));
173173
174174
175- assertThat ("Deep linked examples are dereferenced." ,
176- singleExampleMap (openAPI , "/users" , PathItem ::getPost , "400" , APPLICATION_JSON ).get ("$ref" ).toString (),
177- isComponentExample );
175+ assertComponentExample (openAPI , "/users" , PathItem ::getPost , "400" );
176+
178177 assertThat ("The deep linked example is in /components/examples" ,
179178 openAPI .getComponents ().getExamples ().get ("lib-bad-request-validation-error" ), notNullValue ());
180179
@@ -186,9 +185,7 @@ void testBundleApi() throws OpenAPILoaderException, IOException {
186185 openAPI .getPaths ().get ("/users" ).getPut ().getResponses ().get ("401" ).getContent ().get (APPLICATION_JSON )
187186 .getExamples ().get ("named-bad-example" ).get$ref (), isComponentExample );
188187
189- assertThat ("Relative path works" ,
190- openAPI .getPaths ().get ("/users" ).getPut ().getResponses ().get ("403" ).getContent ().get (APPLICATION_JSON )
191- .getExample (), notNullValue ());
188+ assertComponentExample (openAPI , "/users" , PathItem ::getPut , "403" );
192189
193190 Example exampleNoThree = openAPI .getPaths ().get ("/multi-users" ).getPost ().getRequestBody ().getContent ()
194191 .get (APPLICATION_JSON ).getExamples ().get ("example-number-three" );
@@ -198,6 +195,27 @@ void testBundleApi() throws OpenAPILoaderException, IOException {
198195
199196 }
200197
198+ /**
199+ * Asserts that the specified path/operation/responseCode points to a component/response that has a ref to an
200+ * example and validates that this example exists in components/examples.
201+ * @param openAPI the OpenAPI object
202+ * @param path the path of the endpoint
203+ * @param operationGetter the getter function to retrieve the operation from the path item
204+ * @param responseCode the response code to check
205+ */
206+ private void assertComponentExample (OpenAPI openAPI , String path , Function <PathItem , Operation > operationGetter ,
207+ String responseCode ) {
208+ String responseRef = operationGetter .apply (openAPI .getPaths ().get (path )).getResponses ().get (responseCode ).get$ref ();
209+ assertThat ("Response is referenced" , responseRef , notNullValue ());
210+ ApiResponse response = openAPI .getComponents ().getResponses ().get (substringAfterLast (responseRef , "/" ));
211+ assertThat ("Response " + responseRef + " is inlined in components/response" , response , notNullValue ());
212+ Object exampleRef = ((Map )response .getContent ().get (APPLICATION_JSON ).getExample ()).get ("$ref" );
213+ assertThat ("The component has a ref to the actual example" , exampleRef , notNullValue ());
214+ assertThat ("400 response is inlined in components/response" ,
215+ openAPI .getComponents ().getExamples ().containsKey (
216+ substringAfterLast (String .valueOf (exampleRef ), "/" )), is (true ));
217+ }
218+
201219 private ObjectNode singleExampleNode (OpenAPI openAPI , String path , Function <PathItem , Operation > operation ,
202220 String response , String contentType ) {
203221 return (ObjectNode ) singleExample (openAPI , path , operation , response , contentType );
@@ -214,9 +232,11 @@ private Object singleExample(OpenAPI openAPI, String path, Function<PathItem, Op
214232 ApiResponses responses = apply .getResponses ();
215233 ApiResponse apiResponse = responses .get (response );
216234 Content content = apiResponse .getContent ();
235+ if (content == null ) {
236+ return Map .of ();
237+ }
217238 MediaType mediaType = content .get (contentType );
218- return mediaType
219- .getExample ();
239+ return mediaType .getExample ();
220240 }
221241
222242 // @Test - not really a test.
0 commit comments