From 54bb3824302ff8c619b09da84fcb107c98be1309 Mon Sep 17 00:00:00 2001 From: Simon Bullik Date: Wed, 12 Aug 2020 23:10:39 +0200 Subject: [PATCH 1/8] Properly decode $ref fixes #5720 --- .../codegen/utils/ModelUtils.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index 4c3e9fe01180..e72e8812fe87 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -43,6 +43,7 @@ import java.math.BigDecimal; import java.net.URI; +import java.net.URLDecoder; import java.util.*; import java.util.Map.Entry; import java.util.stream.Collectors; @@ -71,7 +72,7 @@ public class ModelUtils { JSON_MAPPER = ObjectMapperFactory.createJson(); YAML_MAPPER = ObjectMapperFactory.createYaml(); } - + public static void setDisallowAdditionalPropertiesIfNotPresent(boolean value) { GlobalSettings.setProperty(disallowAdditionalPropertiesIfNotPresent, Boolean.toString(value)); } @@ -386,7 +387,7 @@ public static String getSimpleRef(String ref) { } - return ref; + return URLDecoder.decode(ref); } /** @@ -1077,7 +1078,7 @@ public static Schema unaliasSchema(OpenAPI openAPI, /** * Returns the additionalProperties Schema for the specified input schema. - * + * * The additionalProperties keyword is used to control the handling of additional, undeclared * properties, that is, properties whose names are not listed in the properties keyword. * The additionalProperties keyword may be either a boolean or an object. @@ -1085,7 +1086,7 @@ public static Schema unaliasSchema(OpenAPI openAPI, * By default when the additionalProperties keyword is not specified in the input schema, * any additional properties are allowed. This is equivalent to setting additionalProperties * to the boolean value True or setting additionalProperties: {} - * + * * @param openAPI the object that encapsulates the OAS document. * @param schema the input schema that may or may not have the additionalProperties keyword. * @return the Schema of the additionalProperties. The null value is returned if no additional @@ -1141,7 +1142,7 @@ public static Schema getAdditionalProperties(OpenAPI openAPI, Schema schema) { } return null; } - + public static Header getReferencedHeader(OpenAPI openAPI, Header header) { if (header != null && StringUtils.isNotEmpty(header.get$ref())) { String name = getSimpleRef(header.get$ref()); @@ -1478,12 +1479,12 @@ private static ObjectMapper getRightMapper(String data) { /** * Parse and return a JsonNode representation of the input OAS document. - * + * * @param location the URL of the OAS document. * @param auths the list of authorization values to access the remote URL. - * + * * @throws java.lang.Exception if an error occurs while retrieving the OpenAPI document. - * + * * @return A JsonNode representation of the input OAS document. */ public static JsonNode readWithInfo(String location, List auths) throws Exception { @@ -1511,14 +1512,14 @@ public static JsonNode readWithInfo(String location, List au /** * Parse the OAS document at the specified location, get the swagger or openapi version * as specified in the source document, and return the version. - * + * * For OAS 2.0 documents, return the value of the 'swagger' attribute. * For OAS 3.x documents, return the value of the 'openapi' attribute. - * + * * @param openAPI the object that encapsulates the OAS document. * @param location the URL of the OAS document. * @param auths the list of authorization values to access the remote URL. - * + * * @return the version of the OpenAPI document. */ public static SemVer getOpenApiVersion(OpenAPI openAPI, String location, List auths) { From d492ce20f68a48f0ac46ff51a04e9de4e73e4c1f Mon Sep 17 00:00:00 2001 From: Simon Bullik Date: Wed, 12 Aug 2020 23:37:19 +0200 Subject: [PATCH 2/8] Specify decoding encoding --- .../java/org/openapitools/codegen/utils/ModelUtils.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index e72e8812fe87..8049dffc3c1a 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -41,6 +41,7 @@ import org.slf4j.LoggerFactory; import org.apache.commons.io.FileUtils; +import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.net.URI; import java.net.URLDecoder; @@ -387,7 +388,11 @@ public static String getSimpleRef(String ref) { } - return URLDecoder.decode(ref); + try { + return URLDecoder.decode(ref, "UTF8"); + } catch (UnsupportedEncodingException e) { + return ref; + } } /** From 18580bdc67df8e40278e910ac70c94b2f82ab757 Mon Sep 17 00:00:00 2001 From: Simon Bullik Date: Thu, 13 Aug 2020 01:35:28 +0200 Subject: [PATCH 3/8] Nicer syntax --- .../java/org/openapitools/codegen/utils/ModelUtils.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index 8049dffc3c1a..3ebc392fa5bd 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -389,10 +389,11 @@ public static String getSimpleRef(String ref) { } try { - return URLDecoder.decode(ref, "UTF8"); - } catch (UnsupportedEncodingException e) { - return ref; + ref = URLDecoder.decode(ref, "UTF8"); + } catch (UnsupportedEncodingException ignored) { } + + return ref; } /** From 78fb8c3ce455e8101d39a4d12e3ad81ac9248ffc Mon Sep 17 00:00:00 2001 From: Simon Bullik Date: Thu, 13 Aug 2020 01:43:12 +0200 Subject: [PATCH 4/8] UTF-8 typo --- .../main/java/org/openapitools/codegen/utils/ModelUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index 3ebc392fa5bd..d69be9cdb454 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -389,7 +389,7 @@ public static String getSimpleRef(String ref) { } try { - ref = URLDecoder.decode(ref, "UTF8"); + ref = URLDecoder.decode(ref, "UTF-8"); } catch (UnsupportedEncodingException ignored) { } From 5693303a3580ad06fa8ae817256409b6e8052d4f Mon Sep 17 00:00:00 2001 From: Simon Bullik Date: Wed, 26 Aug 2020 11:41:45 +0200 Subject: [PATCH 5/8] Unescape special characters --- .../main/java/org/openapitools/codegen/utils/ModelUtils.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index d69be9cdb454..f2781f9e61b7 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -393,6 +393,8 @@ public static String getSimpleRef(String ref) { } catch (UnsupportedEncodingException ignored) { } + ref = ref.replace("~0", "~").replace("~1", "/"); + return ref; } From e2634d46009f0f477dffdf3517d9d7e268e0638b Mon Sep 17 00:00:00 2001 From: Simon Bullik Date: Wed, 26 Aug 2020 11:54:41 +0200 Subject: [PATCH 6/8] Change order of unescaping to prevent escaped sequences by accident --- .../main/java/org/openapitools/codegen/utils/ModelUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index f2781f9e61b7..c97beed612f3 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -393,7 +393,7 @@ public static String getSimpleRef(String ref) { } catch (UnsupportedEncodingException ignored) { } - ref = ref.replace("~0", "~").replace("~1", "/"); + ref = ref.replace("~1", "/").replace("~0", "~"); return ref; } From b4be8c8496e435fb39d2dcee5d8f05cc53458d54 Mon Sep 17 00:00:00 2001 From: sbu <64100880+sbu-WBT@users.noreply.github.com> Date: Thu, 3 Sep 2020 09:52:49 +0200 Subject: [PATCH 7/8] Comment for special decoding Co-authored-by: Jim Schubert --- .../main/java/org/openapitools/codegen/utils/ModelUtils.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index c97beed612f3..8afa415286bf 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -393,6 +393,11 @@ public static String getSimpleRef(String ref) { } catch (UnsupportedEncodingException ignored) { } + // see https://tools.ietf.org/html/rfc6901#section-3 + // Because the characters '~' (%x7E) and '/' (%x2F) have special meanings in + // JSON Pointer, '~' needs to be encoded as '~0' and '/' needs to be encoded + // as '~1' when these characters appear in a reference token. + // This reverses that encoding. ref = ref.replace("~1", "/").replace("~0", "~"); return ref; From 7353c19b69b833df04b24ed5215440be15e3843c Mon Sep 17 00:00:00 2001 From: Simon Bullik Date: Thu, 3 Sep 2020 10:12:27 +0200 Subject: [PATCH 8/8] Add unit test for simple ref decoding --- .../org/openapitools/codegen/utils/ModelUtilsTest.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java index 221f13a70045..70b63d68b2a8 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java @@ -271,4 +271,10 @@ public void testIsSetFailsForNullSchema() { ArraySchema as = null; Assert.assertFalse(ModelUtils.isSet(as)); } -} \ No newline at end of file + + @Test + public void testSimpleRefDecoding() { + String decoded = ModelUtils.getSimpleRef("#/components/~01%20Hallo~1Welt"); + Assert.assertEquals(decoded, "~1 Hallo/Welt"); + } +}