Skip to content

Commit 63f8ad6

Browse files
authored
Merge pull request #96 from adyen-examples/inline-response-examples
Response inline examples
2 parents 68d4a2e + b6cda74 commit 63f8ad6

File tree

3 files changed

+434
-10
lines changed

3 files changed

+434
-10
lines changed

src/main/java/com/adyen/codegen/postman/PostmanV2Generator.java

+25-10
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,16 @@ List<PostmanResponse> getResponseExamples(CodegenResponse codegenResponse, Strin
312312
String key = entry.getKey();
313313
String ref = entry.getValue().get$ref();
314314

315-
if(ref != null) {
315+
String response;
316+
if (ref != null) {
317+
// get example by $ref
316318
Example example = this.openAPI.getComponents().getExamples().get(extractExampleByName(ref));
317-
String response = new ExampleJsonHelper().getJsonFromExample(example);
318-
postmanResponses.add(new PostmanResponse(key, codegenResponse, message, response));
319+
response = new ExampleJsonHelper().getJsonFromExample(example);
320+
} else {
321+
// get inline example
322+
response = new ExampleJsonHelper().getJsonFromExample(entry.getValue());
319323
}
324+
postmanResponses.add(new PostmanResponse(key, codegenResponse, message, response));
320325
}
321326

322327
} else if (codegenResponse.getContent() != null) {
@@ -339,19 +344,29 @@ List<PostmanRequestItem> getPostmanRequests(CodegenOperation codegenOperation) {
339344
items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().getJsonFromSchema(codegenOperation.bodyParam)));
340345
} else {
341346
// get from examples
342-
if (codegenOperation.bodyParam.example != null) {
343-
// find in bodyParam example
344-
items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().formatJson(codegenOperation.bodyParam.example)));
345-
} else if (codegenOperation.bodyParam.getContent().get("application/json") != null &&
347+
if (codegenOperation.bodyParam.getContent().get("application/json") != null &&
346348
codegenOperation.bodyParam.getContent().get("application/json").getExamples() != null) {
347349
// find in components/examples
348350
for (Map.Entry<String, Example> entry : codegenOperation.bodyParam.getContent().get("application/json").getExamples().entrySet()) {
349351
String exampleRef = entry.getValue().get$ref();
350-
Example example = this.openAPI.getComponents().getExamples().get(extractExampleByName(exampleRef));
351-
String exampleAsString = new ExampleJsonHelper().getJsonFromExample(example);
352352

353-
items.add(new PostmanRequestItem(example.getSummary(), exampleAsString, entry.getKey()));
353+
String exampleAsString;
354+
String exampleName;
355+
if (exampleRef != null) {
356+
// get example by $ref
357+
Example example = this.openAPI.getComponents().getExamples().get(extractExampleByName(exampleRef));
358+
exampleAsString = new ExampleJsonHelper().getJsonFromExample(example);
359+
exampleName = example.getSummary();
360+
} else {
361+
// get inline example
362+
exampleAsString = new ExampleJsonHelper().getJsonFromExample(entry.getValue());
363+
exampleName = entry.getValue().getSummary();
364+
}
365+
items.add(new PostmanRequestItem(exampleName, exampleAsString, entry.getKey()));
354366
}
367+
} else if (codegenOperation.bodyParam.example != null) {
368+
// find in bodyParam example
369+
items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().formatJson(codegenOperation.bodyParam.example)));
355370
} else if (codegenOperation.bodyParam.getSchema() != null) {
356371
// find in schema example
357372
String exampleAsString = new ExampleJsonHelper().formatJson(codegenOperation.bodyParam.getSchema().getExample());

src/test/java/com/adyen/codegen/postman/PostmanV2GeneratorTest.java

+37
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public void testConfigWithCreationPostmanVariables() throws Exception {
6464
Assert.assertTrue(postmanV2Generator.isCreatePostmanVariables());
6565
Assert.assertArrayEquals(postmanV2Generator.postmanVariableNames, new String[]{"VAR1", "VAR2", "VAR3"});
6666
}
67+
6768
@Test
6869
public void testBasicGeneration() throws IOException {
6970

@@ -862,4 +863,40 @@ public void testResponses() throws IOException {
862863

863864
}
864865

866+
@Test
867+
public void testInlineExamples() throws IOException {
868+
869+
File output = Files.createTempDirectory("postmantest_").toFile();
870+
output.deleteOnExit();
871+
872+
final CodegenConfigurator configurator = new CodegenConfigurator()
873+
.setGeneratorName("postman-v2")
874+
.setInputSpec("./src/test/resources/ForeignExchangeService-v1.json")
875+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
876+
877+
final ClientOptInput clientOptInput = configurator.toClientOptInput();
878+
DefaultGenerator generator = new DefaultGenerator();
879+
List<File> files = generator.opts(clientOptInput).generate();
880+
881+
System.out.println(files);
882+
files.forEach(File::deleteOnExit);
883+
884+
Path path = Paths.get(output + "/postman.json");
885+
TestUtils.assertFileExists(path);
886+
TestUtils.assertFileContains(path, "\"schema\": \"https://schema.getpostman.com/json/collection/v2.1.0/collection.json\"");
887+
888+
// verify request name (from summary)
889+
TestUtils.assertFileContains(path, "\"name\": \"Foreign Exchange API\"");
890+
// verify request endpoint
891+
TestUtils.assertFileContains(path, "\"name\": \"/rates/calculate\",");
892+
// verify response is included
893+
TestUtils.assertFileContains(path, "\"response\": [\n" +
894+
" {\"name\": \"Successful operation\",\n" +
895+
" \"code\": 200,\n" +
896+
" \"status\": \"OK\",\n" +
897+
" \"header\": [{\n" +
898+
" \"key\": \"Content-Type\",\n" +
899+
" \"value\": \"application/json\"}\n");
900+
}
901+
865902
}

0 commit comments

Comments
 (0)