From 30d6a2ff804ab4527778ac8f42d1c9f6ae2189dc Mon Sep 17 00:00:00 2001 From: Beppe Catanese <1771700+gcatanese@users.noreply.github.com> Date: Fri, 7 Jul 2023 19:05:53 +0200 Subject: [PATCH] [POSTMAN] Use parameter default value (#16027) * Refactor to use defaultValue * Test parameter with defaultValue * Update samples --- .../languages/PostmanCollectionCodegen.java | 22 ++++---- .../postman-collection/postman.mustache | 2 +- .../postman/PostmanCollectionCodegenTest.java | 13 ++++- .../3_0/postman-collection/SampleProject.yaml | 39 ++++++++++++++ .../schema/postman-collection/postman.json | 54 ++++++++++++++++++- 5 files changed, 115 insertions(+), 15 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PostmanCollectionCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PostmanCollectionCodegen.java index 700e41a6d4f..9eb9ef63a86 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PostmanCollectionCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PostmanCollectionCodegen.java @@ -135,7 +135,7 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC variables.add(new PostmanVariable() .addName(parameter.paramName) .addType(mapToPostmanType(parameter.dataType)) - .addExample(parameter.example)); + .addeDefaultValue(parameter.defaultValue)); } } @@ -152,7 +152,7 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC variables.entrySet().stream().forEach(serverVariableEntry -> this.variables.add(new PostmanVariable() .addName(serverVariableEntry.getKey()) .addType("string") - .addExample(serverVariableEntry.getValue().getDefault()))); + .addeDefaultValue(serverVariableEntry.getValue().getDefault()))); } return super.fromServerVariables(variables); @@ -402,7 +402,7 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC variables.add(new PostmanVariable() .addName(var) .addType("string") - .addExample("")); + .addeDefaultValue("")); } } @@ -799,7 +799,7 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC private String name; private String type; - private String example; + private String defaultValue; public PostmanVariable addName(String name) { this.name = name; @@ -811,8 +811,8 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC return this; } - public PostmanVariable addExample(String example) { - this.example = example; + public PostmanVariable addeDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; return this; } @@ -832,12 +832,12 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC this.type = type; } - public String getExample() { - return example; + public String getDefaultValue() { + return defaultValue; } - public void setExample(String example) { - this.example = example; + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; } @Override @@ -858,7 +858,7 @@ public class PostmanCollectionCodegen extends DefaultCodegen implements CodegenC return "PostmanVariable{" + "name='" + name + '\'' + ", type='" + type + '\'' + - ", example='" + example + '\'' + + ", defaultValue='" + defaultValue + '\'' + '}'; } } diff --git a/modules/openapi-generator/src/main/resources/postman-collection/postman.mustache b/modules/openapi-generator/src/main/resources/postman-collection/postman.mustache index ddf03f69ae7..d99e8420d66 100644 --- a/modules/openapi-generator/src/main/resources/postman-collection/postman.mustache +++ b/modules/openapi-generator/src/main/resources/postman-collection/postman.mustache @@ -66,7 +66,7 @@ { {{! use first element in vendorExtensions }} "key": "{{name}}", - "value": "{{example}}", + "value": "{{defaultValue}}", "type": "{{type}}" }{{^-last}},{{/-last}}{{/variables}}{{/-first}}{{/vendorExtensions}}{{/apis}}{{/apiInfo}} ] diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/postman/PostmanCollectionCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/postman/PostmanCollectionCodegenTest.java index fa7225826f8..4f679903b5e 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/postman/PostmanCollectionCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/postman/PostmanCollectionCodegenTest.java @@ -138,13 +138,22 @@ public class PostmanCollectionCodegenTest { files.forEach(File::deleteOnExit); - assertFileExists(Paths.get(output + "/postman.json")); + Path path = Paths.get(output + "/postman.json"); + assertFileExists(path); ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(new File(output + "/postman.json")); // verify json has variables assertTrue(jsonNode.get("variable") instanceof ArrayNode); - assertEquals(5, ((ArrayNode) jsonNode.get("variable")).size()); + assertEquals(6, (jsonNode.get("variable")).size()); + + // verify param userId (without default value) + TestUtils.assertFileContains(path, + "key\": \"userId\", \"value\": \"\", \"type\": \"number\""); + // verify param groupId (with default value) + TestUtils.assertFileContains(path, + "key\": \"groupId\", \"value\": \"1\", \"type\": \"number\""); + } @Test diff --git a/modules/openapi-generator/src/test/resources/3_0/postman-collection/SampleProject.yaml b/modules/openapi-generator/src/test/resources/3_0/postman-collection/SampleProject.yaml index 97786ebeeb4..661afd60780 100644 --- a/modules/openapi-generator/src/test/resources/3_0/postman-collection/SampleProject.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/postman-collection/SampleProject.yaml @@ -244,6 +244,31 @@ paths: description: Create a new user. tags: - basic + '/groups/{groupId}': + get: + summary: Get group by ID + tags: + - advanced + parameters: + - description: group Id + name: groupId + in: path + required: true + schema: + type: integer + default: 1 + responses: + '200': + description: Group Found + content: + application/json: + schema: + $ref: '#/components/schemas/Group' + '404': + description: Group Not Found + operationId: get-groups-groupId + description: Get group of users + components: securitySchemes: BasicAuth: @@ -303,6 +328,20 @@ components: - lastName - email - emailVerified + Group: + title: Group + type: object + description: '' + properties: + id: + type: integer + description: Unique identifier for the given group. + name: + type: string + example: admin + required: + - id + - name examples: get-user-basic: summary: Example request for Get User diff --git a/samples/schema/postman-collection/postman.json b/samples/schema/postman-collection/postman.json index a0951aaa1c7..e787aceb749 100644 --- a/samples/schema/postman-collection/postman.json +++ b/samples/schema/postman-collection/postman.json @@ -77,6 +77,53 @@ "name": "advanced", "item": [ { + "name": "/groups/{{groupId}}", + "description": "Get group of users", + "item": [ + { + "name": "Get group by ID", + "request": { + "method": "GET", + "header": [ + { + "key": "Accept", + "value": "application/json" + } + ], + "body": { + "mode": "raw", + "raw": "", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "{{baseUrl}}/groups/{{groupId}}", + "host": [ + "{{baseUrl}}" + ], + "path": [ + "groups", + "{{groupId}}" + ], + "variable": [ + { + "key": "groupId", + "value": "", + "description": "group Id" + } + ], + "query": [ + ] + }, + "description": "Get group of users" + } + } + ] + }, + { "name": "/users/{{userId}}", "description": "Retrieve the information of the user with the matching user ID.", "item": [ @@ -268,6 +315,11 @@ "value": "v1", "type": "string" }, + { + "key": "groupId", + "value": "1", + "type": "number" + }, { "key": "port", "value": "5000", @@ -275,7 +327,7 @@ }, { "key": "userId", - "value": "a", + "value": "", "type": "number" } ]