From 6f57e38629d98a231209475b04a4a735b2a16070 Mon Sep 17 00:00:00 2001 From: Bryan Cross Date: Tue, 18 Apr 2023 08:29:28 -0500 Subject: [PATCH] Ensure JSON comments in body are handled --- .../com/postman/collection/Collection.java | 2 + .../postman/collection/CollectionElement.java | 6 +- ...body-with-comments.postman_collection.json | 105 ++++++++++++++++++ ...y-with-no-comments.postman_collection.json | 105 ++++++++++++++++++ .../java/com/postman/collection/AppTest.java | 20 +++- 5 files changed, 236 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/com/postman/collection/body-with-comments.postman_collection.json create mode 100644 src/main/resources/com/postman/collection/body-with-no-comments.postman_collection.json diff --git a/src/main/java/com/postman/collection/Collection.java b/src/main/java/com/postman/collection/Collection.java index 36e9d61..407bcdd 100644 --- a/src/main/java/com/postman/collection/Collection.java +++ b/src/main/java/com/postman/collection/Collection.java @@ -504,6 +504,8 @@ public static Collection pmcFactory(String json) { gsonBuilder.registerTypeAdapter(Item.class, new com.postman.collection.adapter.ItemDeserializer()); gsonBuilder.registerTypeAdapter(ItemGroup.class, new com.postman.collection.adapter.ItemGroupDeserializer()); pmcRetVal = gsonBuilder.create().fromJson(json, Collection.class); + //Added to hopefully allow JSON comments + //gsonBuilder.setLenient(); pmcRetVal.init(); return pmcRetVal; diff --git a/src/main/java/com/postman/collection/CollectionElement.java b/src/main/java/com/postman/collection/CollectionElement.java index 2a2bb96..cb0b153 100644 --- a/src/main/java/com/postman/collection/CollectionElement.java +++ b/src/main/java/com/postman/collection/CollectionElement.java @@ -145,9 +145,13 @@ public boolean validate(String altSchemaJSON) throws ValidationException { { valMsg = vm.getMessage(); valPath = vm.getPath(); - + //Ignore `default` keyword ignoreValidationErrors = ignoreValidationErrors && valMsg.contains("$.variable[") && valMsg.contains(".type: does not have a value in the enumeration [string, boolean, any, number]"); + //Ignore "JSON comments" } + + + return ((this.validationMessages == null || this.validationMessages.size() == 0) || ignoreValidationErrors); } diff --git a/src/main/resources/com/postman/collection/body-with-comments.postman_collection.json b/src/main/resources/com/postman/collection/body-with-comments.postman_collection.json new file mode 100644 index 0000000..f0c6aff --- /dev/null +++ b/src/main/resources/com/postman/collection/body-with-comments.postman_collection.json @@ -0,0 +1,105 @@ +{ + "info": { + "_postman_id": "422e93b7-b31b-4384-9e80-28c1b8e743c6", + "name": "Body With Comments", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "23889826", + "_collection_link": "https://v10-technical-enablement.postman.co/workspace/JPostman~d827e22e-3694-408d-b18b-d3eea108c001/collection/23889826-422e93b7-b31b-4384-9e80-28c1b8e743c6?action=share&creator=23889826&source=collection_link" + }, + "item": [ + { + "name": "Raw-text JSON", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"json-field1\":\"value 1\"\n /* JSON Comment */\n ,\"json-field2\":\"value 2\"\n //another JSON comment\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "https://postman-echo.com/post", + "protocol": "https", + "host": [ + "postman-echo", + "com" + ], + "path": [ + "post" + ] + } + }, + "response": [ + { + "name": "NORMAL Raw-text JSON", + "originalRequest": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"json-field1\":\"value 1\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "https://postman-echo.com/post", + "protocol": "https", + "host": [ + "postman-echo", + "com" + ], + "path": [ + "post" + ] + } + }, + "status": "OK", + "code": 200, + "_postman_previewlanguage": "text", + "header": [], + "cookie": [], + "body": "THis is the expected response" + } + ] + } + ], + "event": [ + { + "listen": "prerequest", + "script": { + "type": "text/javascript", + "exec": [ + "" + ] + } + }, + { + "listen": "test", + "script": { + "type": "text/javascript", + "exec": [ + "" + ] + } + } + ], + "variable": [ + { + "key": "field3value", + "value": "field-3 variable value", + "type": "string" + }, + { + "key": "Xfield3value", + "value": "Xfield-3 variable value", + "type": "string" + } + ] +} \ No newline at end of file diff --git a/src/main/resources/com/postman/collection/body-with-no-comments.postman_collection.json b/src/main/resources/com/postman/collection/body-with-no-comments.postman_collection.json new file mode 100644 index 0000000..efe3fe3 --- /dev/null +++ b/src/main/resources/com/postman/collection/body-with-no-comments.postman_collection.json @@ -0,0 +1,105 @@ +{ + "info": { + "_postman_id": "9a5d446d-b91f-4bb3-a59e-0bd183e68143", + "name": "Body With Comments Copy", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "23889826", + "_collection_link": "https://v10-technical-enablement.postman.co/workspace/JPostman~d827e22e-3694-408d-b18b-d3eea108c001/collection/23889826-9a5d446d-b91f-4bb3-a59e-0bd183e68143?action=share&creator=23889826&source=collection_link" + }, + "item": [ + { + "name": "Raw-text JSON", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"json-field1\":\"value 1\"\n \n ,\"json-field2\":\"value 2\"\n \n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "https://postman-echo.com/post", + "protocol": "https", + "host": [ + "postman-echo", + "com" + ], + "path": [ + "post" + ] + } + }, + "response": [ + { + "name": "NORMAL Raw-text JSON", + "originalRequest": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\n \"json-field1\":\"value 1\"\n}", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "https://postman-echo.com/post", + "protocol": "https", + "host": [ + "postman-echo", + "com" + ], + "path": [ + "post" + ] + } + }, + "status": "OK", + "code": 200, + "_postman_previewlanguage": "text", + "header": [], + "cookie": [], + "body": "THis is the expected response" + } + ] + } + ], + "event": [ + { + "listen": "prerequest", + "script": { + "type": "text/javascript", + "exec": [ + "" + ] + } + }, + { + "listen": "test", + "script": { + "type": "text/javascript", + "exec": [ + "" + ] + } + } + ], + "variable": [ + { + "key": "field3value", + "value": "field-3 variable value", + "type": "string" + }, + { + "key": "Xfield3value", + "value": "Xfield-3 variable value", + "type": "string" + } + ] +} \ No newline at end of file diff --git a/src/test/java/com/postman/collection/AppTest.java b/src/test/java/com/postman/collection/AppTest.java index 102b7da..a71b572 100644 --- a/src/test/java/com/postman/collection/AppTest.java +++ b/src/test/java/com/postman/collection/AppTest.java @@ -1608,4 +1608,22 @@ public void testWriteToPostman() { } -} \ No newline at end of file + @Test + public void testValidateWithBodyComments() { + + boolean valid = false; + + + + try { + pmcTest = Collection.pmcFactory(new File(filePath + resourcePath + "/body-with-no-comments.postman_collection.json")); + valid = pmcTest.validate(); + } + catch(Exception e) { + e.printStackTrace(); + } + + + + +}} \ No newline at end of file