Skip to content

Commit af700ac

Browse files
committed
Adjust test to updated parser behaviour, small simplification refactoring.
1 parent 4b0fe73 commit af700ac

File tree

3 files changed

+39
-18
lines changed

3 files changed

+39
-18
lines changed

boat-engine/src/main/java/com/backbase/oss/boat/transformers/bundler/BoatOpenAPIResolver.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.swagger.v3.parser.processors.ComponentsProcessor;
1010
import io.swagger.v3.parser.processors.OperationProcessor;
1111
import io.swagger.v3.parser.processors.PathsProcessor;
12+
1213
import java.util.Iterator;
1314
import java.util.List;
1415

@@ -40,14 +41,13 @@ public BoatOpenAPIResolver(OpenAPI openApi, List<AuthorizationValue> auths, Stri
4041
public OpenAPI resolve() {
4142
if (this.openApi == null) {
4243
return null;
43-
} else {
44-
examplesProcessor.processExamples(this.openApi);
45-
processOpenAPI();
46-
return this.openApi;
4744
}
45+
processOpenAPI();
46+
return this.openApi;
4847
}
4948

5049
private void processOpenAPI() {
50+
this.examplesProcessor.processExamples();
5151
this.pathProcessor.processPaths();
5252
this.componentsProcessor.processComponents();
5353
if (this.openApi.getPaths() == null) {

boat-engine/src/main/java/com/backbase/oss/boat/transformers/bundler/ExamplesProcessor.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import io.swagger.v3.oas.models.media.MediaType;
1616
import io.swagger.v3.parser.models.RefFormat;
1717
import io.swagger.v3.parser.util.RefUtils;
18+
import lombok.extern.slf4j.Slf4j;
19+
import org.apache.commons.lang3.StringUtils;
20+
1821
import java.io.IOException;
1922
import java.net.URI;
2023
import java.nio.file.Files;
@@ -24,8 +27,6 @@
2427
import java.util.Map;
2528
import java.util.Objects;
2629
import java.util.Optional;
27-
import lombok.extern.slf4j.Slf4j;
28-
import org.apache.commons.lang3.StringUtils;
2930

3031

3132
@Slf4j
@@ -53,7 +54,7 @@ public ExamplesProcessor(OpenAPI openAPI, String inputUri) {
5354
}
5455
}
5556

56-
public void processExamples(OpenAPI openAPI) {
57+
public void processExamples() {
5758

5859
log.debug("Processing examples in Components");
5960
// dereference the /component/examples first...

boat-engine/src/test/java/com/backbase/oss/boat/transformers/BundlerTests.java

+31-11
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.Map;
3333
import java.util.function.Function;
3434

35+
import static org.apache.commons.lang3.StringUtils.substringAfterLast;
3536
import static org.hamcrest.CoreMatchers.is;
3637
import static org.hamcrest.CoreMatchers.notNullValue;
3738
import 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

Comments
 (0)