Skip to content

Response inline examples #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions src/main/java/com/adyen/codegen/postman/PostmanV2Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,16 @@ List<PostmanResponse> getResponseExamples(CodegenResponse codegenResponse, Strin
String key = entry.getKey();
String ref = entry.getValue().get$ref();

if(ref != null) {
String response;
if (ref != null) {
// get example by $ref
Example example = this.openAPI.getComponents().getExamples().get(extractExampleByName(ref));
String response = new ExampleJsonHelper().getJsonFromExample(example);
postmanResponses.add(new PostmanResponse(key, codegenResponse, message, response));
response = new ExampleJsonHelper().getJsonFromExample(example);
} else {
// get inline example
response = new ExampleJsonHelper().getJsonFromExample(entry.getValue());
}
postmanResponses.add(new PostmanResponse(key, codegenResponse, message, response));
}

} else if (codegenResponse.getContent() != null) {
Expand All @@ -339,19 +344,29 @@ List<PostmanRequestItem> getPostmanRequests(CodegenOperation codegenOperation) {
items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().getJsonFromSchema(codegenOperation.bodyParam)));
} else {
// get from examples
if (codegenOperation.bodyParam.example != null) {
// find in bodyParam example
items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().formatJson(codegenOperation.bodyParam.example)));
} else if (codegenOperation.bodyParam.getContent().get("application/json") != null &&
if (codegenOperation.bodyParam.getContent().get("application/json") != null &&
codegenOperation.bodyParam.getContent().get("application/json").getExamples() != null) {
// find in components/examples
for (Map.Entry<String, Example> entry : codegenOperation.bodyParam.getContent().get("application/json").getExamples().entrySet()) {
String exampleRef = entry.getValue().get$ref();
Example example = this.openAPI.getComponents().getExamples().get(extractExampleByName(exampleRef));
String exampleAsString = new ExampleJsonHelper().getJsonFromExample(example);

items.add(new PostmanRequestItem(example.getSummary(), exampleAsString, entry.getKey()));
String exampleAsString;
String exampleName;
if (exampleRef != null) {
// get example by $ref
Example example = this.openAPI.getComponents().getExamples().get(extractExampleByName(exampleRef));
exampleAsString = new ExampleJsonHelper().getJsonFromExample(example);
exampleName = example.getSummary();
} else {
// get inline example
exampleAsString = new ExampleJsonHelper().getJsonFromExample(entry.getValue());
exampleName = entry.getValue().getSummary();
}
items.add(new PostmanRequestItem(exampleName, exampleAsString, entry.getKey()));
}
} else if (codegenOperation.bodyParam.example != null) {
// find in bodyParam example
items.add(new PostmanRequestItem(codegenOperation.summary, new ExampleJsonHelper().formatJson(codegenOperation.bodyParam.example)));
} else if (codegenOperation.bodyParam.getSchema() != null) {
// find in schema example
String exampleAsString = new ExampleJsonHelper().formatJson(codegenOperation.bodyParam.getSchema().getExample());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void testConfigWithCreationPostmanVariables() throws Exception {
Assert.assertTrue(postmanV2Generator.isCreatePostmanVariables());
Assert.assertArrayEquals(postmanV2Generator.postmanVariableNames, new String[]{"VAR1", "VAR2", "VAR3"});
}

@Test
public void testBasicGeneration() throws IOException {

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

}

@Test
public void testInlineExamples() throws IOException {

File output = Files.createTempDirectory("postmantest_").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("postman-v2")
.setInputSpec("./src/test/resources/ForeignExchangeService-v1.json")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

final ClientOptInput clientOptInput = configurator.toClientOptInput();
DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(clientOptInput).generate();

System.out.println(files);
files.forEach(File::deleteOnExit);

Path path = Paths.get(output + "/postman.json");
TestUtils.assertFileExists(path);
TestUtils.assertFileContains(path, "\"schema\": \"https://schema.getpostman.com/json/collection/v2.1.0/collection.json\"");

// verify request name (from summary)
TestUtils.assertFileContains(path, "\"name\": \"Foreign Exchange API\"");
// verify request endpoint
TestUtils.assertFileContains(path, "\"name\": \"/rates/calculate\",");
// verify response is included
TestUtils.assertFileContains(path, "\"response\": [\n" +
" {\"name\": \"Successful operation\",\n" +
" \"code\": 200,\n" +
" \"status\": \"OK\",\n" +
" \"header\": [{\n" +
" \"key\": \"Content-Type\",\n" +
" \"value\": \"application/json\"}\n");
}

}
Loading
Loading