32
32
import java .util .Map ;
33
33
import java .util .function .Function ;
34
34
35
+ import static org .apache .commons .lang3 .StringUtils .substringAfterLast ;
35
36
import static org .hamcrest .CoreMatchers .is ;
36
37
import static org .hamcrest .CoreMatchers .notNullValue ;
37
38
import static org .hamcrest .MatcherAssert .assertThat ;
@@ -120,7 +121,7 @@ void testExamplesProcessor() throws OpenAPILoaderException, URISyntaxException {
120
121
String spec = System .getProperty ("spec" , inputUri );
121
122
OpenAPI openAPI = OpenAPILoader .load (spec );
122
123
OpenAPI openAPIUnproccessed = openAPI ;
123
- new ExamplesProcessor (openAPI ,spec ).processExamples (openAPI );
124
+ new ExamplesProcessor (openAPI ,spec ).processExamples ();
124
125
assertEquals (openAPIUnproccessed , openAPI );
125
126
}
126
127
@@ -131,7 +132,7 @@ void testExamplesProcessorComponentError() throws OpenAPILoaderException {
131
132
OpenAPI openAPI = OpenAPILoader .load (spec );
132
133
ExamplesProcessor examplesProcessor = new ExamplesProcessor (openAPI ,spec );
133
134
try {
134
- examplesProcessor .processExamples (openAPI );
135
+ examplesProcessor .processExamples ();
135
136
fail ("Expected TransformerException" );
136
137
}catch (TransformerException e ){
137
138
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 {
166
167
openAPI .getComponents ().getExamples ().get ("example-in-components-1" ).getSummary (),
167
168
is ("component-examples with example - should be left alone" ));
168
169
169
-
170
170
// actual input is not valid... when there is a ref no other properties should be set - still allow it.
171
171
assertThat ("Component example that duplicates a inline example, is left alone. But the summary is removed" ,
172
172
openAPI .getComponents ().getExamples ().get ("example-number-one" ).getSummary (), is ("example-number-one" ));
173
173
174
174
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
+
178
177
assertThat ("The deep linked example is in /components/examples" ,
179
178
openAPI .getComponents ().getExamples ().get ("lib-bad-request-validation-error" ), notNullValue ());
180
179
@@ -186,9 +185,7 @@ void testBundleApi() throws OpenAPILoaderException, IOException {
186
185
openAPI .getPaths ().get ("/users" ).getPut ().getResponses ().get ("401" ).getContent ().get (APPLICATION_JSON )
187
186
.getExamples ().get ("named-bad-example" ).get$ref (), isComponentExample );
188
187
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" );
192
189
193
190
Example exampleNoThree = openAPI .getPaths ().get ("/multi-users" ).getPost ().getRequestBody ().getContent ()
194
191
.get (APPLICATION_JSON ).getExamples ().get ("example-number-three" );
@@ -198,6 +195,27 @@ void testBundleApi() throws OpenAPILoaderException, IOException {
198
195
199
196
}
200
197
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
+
201
219
private ObjectNode singleExampleNode (OpenAPI openAPI , String path , Function <PathItem , Operation > operation ,
202
220
String response , String contentType ) {
203
221
return (ObjectNode ) singleExample (openAPI , path , operation , response , contentType );
@@ -214,9 +232,11 @@ private Object singleExample(OpenAPI openAPI, String path, Function<PathItem, Op
214
232
ApiResponses responses = apply .getResponses ();
215
233
ApiResponse apiResponse = responses .get (response );
216
234
Content content = apiResponse .getContent ();
235
+ if (content == null ) {
236
+ return Map .of ();
237
+ }
217
238
MediaType mediaType = content .get (contentType );
218
- return mediaType
219
- .getExample ();
239
+ return mediaType .getExample ();
220
240
}
221
241
222
242
// @Test - not really a test.
0 commit comments