Skip to content

Commit e58f19c

Browse files
authored
[POSTMAN] Process request with array of string (#18159)
* Process JSON array * Update test file * Regenerate samples * Add CI test
1 parent 8152052 commit e58f19c

5 files changed

Lines changed: 94 additions & 1 deletion

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PostmanCollectionCodegen.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,8 @@ private String traverseMap(LinkedHashMap<String, Object> linkedHashMap, String r
756756
} else if (value instanceof LinkedHashMap) {
757757
String in = ret + JSON_ESCAPE_DOUBLE_QUOTE + key + JSON_ESCAPE_DOUBLE_QUOTE + ": ";
758758
ret = traverseMap(((LinkedHashMap<String, Object>) value), in);
759+
} else if (value instanceof ArrayList<?>) {
760+
ret = ret + JSON_ESCAPE_DOUBLE_QUOTE + key + JSON_ESCAPE_DOUBLE_QUOTE + ": " + getJsonArray((ArrayList<Object>) value);
759761
} else {
760762
LOGGER.warn("Value type unrecognised: " + value.getClass());
761763
}
@@ -772,6 +774,38 @@ private String traverseMap(LinkedHashMap<String, Object> linkedHashMap, String r
772774
return ret;
773775
}
774776

777+
String getJsonArray(ArrayList<Object> list) {
778+
String ret = "";
779+
780+
for(Object element: list) {
781+
if(element instanceof String) {
782+
ret = ret + getStringArrayElement((String) element) + ", ";
783+
} else if(element instanceof LinkedHashMap) {
784+
ret = traverseMap((LinkedHashMap<String, Object>) element, ret) + ", ";
785+
}
786+
}
787+
788+
if(ret.endsWith(", ")) {
789+
ret = ret.substring(0, ret.length() - 2);
790+
}
791+
792+
return "[" + ret + "]";
793+
}
794+
795+
String getStringArrayElement(String element) {
796+
String ret = "";
797+
798+
if(element.startsWith("{")) {
799+
// isJson (escape all double quotes)
800+
ret = ret + element.replace("\"", JSON_ESCAPE_DOUBLE_QUOTE);
801+
} else {
802+
// string element (add escaped double quotes)
803+
ret = ret + JSON_ESCAPE_DOUBLE_QUOTE + element + JSON_ESCAPE_DOUBLE_QUOTE;
804+
}
805+
806+
return ret;
807+
}
808+
775809
public String getPostmanType(CodegenProperty codegenProperty) {
776810
if(codegenProperty.isNumeric) {
777811
return "number";

modules/openapi-generator/src/test/java/org/openapitools/codegen/postman/PostmanCollectionCodegenTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,46 @@ public void convertNestedLinkedHashMapToJson() {
633633

634634
}
635635

636+
@Test
637+
public void convertNestedArrayListToJson() {
638+
639+
final String EXPECTED =
640+
"{\\n " +
641+
"\\\"id\\\": 1,\\n \\\"city\\\": \\\"Amsterdam\\\",\\n " +
642+
"\\\"tags\\\": [\\\"ams\\\", \\\"adam\\\"]" +
643+
"\\n}";
644+
645+
LinkedHashMap<String, Object> city = new LinkedHashMap<>();
646+
city.put("id", 1);
647+
city.put("city", "Amsterdam");
648+
ArrayList<String> tags = new ArrayList<>();
649+
tags.add("ams");
650+
tags.add("adam");
651+
city.put("tags", tags);
652+
653+
assertEquals(EXPECTED, new PostmanCollectionCodegen().convertToJson(city));
654+
655+
}
656+
657+
@Test
658+
public void convertNestedEmptyArrayListToJson() {
659+
660+
final String EXPECTED =
661+
"{\\n " +
662+
"\\\"id\\\": 1,\\n \\\"city\\\": \\\"Amsterdam\\\",\\n " +
663+
"\\\"tags\\\": []" +
664+
"\\n}";
665+
666+
LinkedHashMap<String, Object> city = new LinkedHashMap<>();
667+
city.put("id", 1);
668+
city.put("city", "Amsterdam");
669+
ArrayList<String> tags = new ArrayList<>();
670+
city.put("tags", tags);
671+
672+
assertEquals(EXPECTED, new PostmanCollectionCodegen().convertToJson(city));
673+
674+
}
675+
636676
@Test
637677
public void testAddToList() {
638678

modules/openapi-generator/src/test/resources/3_0/postman-collection/SampleProject.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ components:
322322
format: date
323323
description: The date that the user was created.
324324
example: '2019-08-24'
325+
tags:
326+
type: array
327+
items:
328+
type: string
329+
description: Tags assigned to the user
325330
required:
326331
- id
327332
- firstName
@@ -353,6 +358,10 @@ components:
353358
dateOfBirth: '1997-10-31'
354359
emailVerified: true
355360
createDate: '2019-08-24'
361+
tags:
362+
- user
363+
- admin
364+
- guest
356365
tags:
357366
- name: basic
358367
description: Basic tag

samples/schema/postman-collection/postman.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@
345345
],
346346
"body": {
347347
"mode": "raw",
348-
"raw": "{\n \"id\" : 777,\n \"firstName\" : \"Alotta\",\n \"lastName\" : \"Rotta\",\n \"email\" : \"alotta.rotta@gmail.com\",\n \"dateOfBirth\" : \"1997-10-31\",\n \"emailVerified\" : true,\n \"createDate\" : \"2019-08-24\"\n}",
348+
"raw": "{\n \"id\" : 777,\n \"firstName\" : \"Alotta\",\n \"lastName\" : \"Rotta\",\n \"email\" : \"alotta.rotta@gmail.com\",\n \"dateOfBirth\" : \"1997-10-31\",\n \"emailVerified\" : true,\n \"createDate\" : \"2019-08-24\",\n \"tags\" : [ \"user\", \"admin\", \"guest\" ]\n}",
349349
"options": {
350350
"raw": {
351351
"language": "json"

samples/schema/postman-collection/python/test/test_endpoints.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ def test_request_from_inline_examples(self):
2929
self.assertEqual(item['request']["method"], 'PATCH')
3030
self.assertEqual(item['request']["body"]["raw"], '{\n "firstName" : "Rebecca"\n}')
3131

32+
def test_request_with_array_strings(self):
33+
# item
34+
item = self.json_data['item'][2]['item'][0]['item'][0]
35+
self.assertEqual(item['request']["method"], 'POST')
36+
data = json.loads(item['request']["body"]["raw"])
37+
# check is list
38+
self.assertTrue(isinstance(data.get("tags"), list))
39+
# check values
40+
self.assertTrue(set(data.get("tags")) == {"user", "admin", "guest"})
41+
3242

3343
if __name__ == '__main__':
3444
unittest.main()

0 commit comments

Comments
 (0)