-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Encode property names when building path to property (#1465)
* Encode property names when building path to property When creating a fragment with JSON Pointer path, we now encode property names using the rules from RFC 6901. This means that the property name can be treated as a single token, even if it contains special characters like # or /. Closes #1402
- Loading branch information
1 parent
82a1352
commit 9315b7b
Showing
7 changed files
with
130 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/JsonPointerUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Copyright © 2010-2020 Nokia | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jsonschema2pojo; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
import java.util.Map; | ||
import java.util.Collections; | ||
|
||
public class JsonPointerUtils { | ||
|
||
private static Map<String,String> SUBSTITUTIONS = new LinkedHashMap<String, String>() {{ | ||
put("~", "~0"); | ||
put("/", "~1"); | ||
put("#", "~2"); | ||
put(".", "~3"); | ||
}}; | ||
|
||
public static String encodeReferenceToken(final String s) { | ||
String encoded = s; | ||
for (Map.Entry<String,String> sub : SUBSTITUTIONS.entrySet()) { | ||
encoded = encoded.replace(sub.getKey(), sub.getValue()); | ||
} | ||
return encoded; | ||
} | ||
|
||
public static String decodeReferenceToken(final String s) { | ||
String decoded = s; | ||
|
||
List<String> reverseOrderedKeys = new ArrayList<String>(SUBSTITUTIONS.keySet()); | ||
Collections.reverse(reverseOrderedKeys); | ||
for (String key : reverseOrderedKeys) { | ||
decoded = decoded.replace(SUBSTITUTIONS.get(key), key); | ||
} | ||
|
||
return decoded; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
jsonschema2pojo-core/src/test/java/org/jsonschema2pojo/JsonPointerUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Copyright © 2010-2020 Nokia | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jsonschema2pojo; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
|
||
|
||
public class JsonPointerUtilsTest { | ||
|
||
@Test | ||
public void testEncodeReferenceToken() { | ||
assertThat(JsonPointerUtils.encodeReferenceToken("com/vsv#..."), is("com~1vsv~2~3~3~3")); | ||
assertThat(JsonPointerUtils.encodeReferenceToken("~1~2~01~3"), is("~01~02~001~03")); | ||
} | ||
|
||
@Test | ||
public void testDecodeReferenceToken() { | ||
assertThat(JsonPointerUtils.decodeReferenceToken("com~1vsv~2~3~3~3"), is("com/vsv#...")); | ||
assertThat(JsonPointerUtils.decodeReferenceToken("~01~02~001~03"), is("~1~2~01~3")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...tegration-tests/src/test/resources/schema/properties/propertiesWithSpecialCharacters.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"type" : "object", | ||
"properties" : { | ||
"VERSV#" : { | ||
"type" : "string" | ||
}, | ||
"foo#bar" : { | ||
"type" : "string" | ||
}, | ||
"${RFC_NUMBER}" : { | ||
"type" : "string" | ||
}, | ||
"org.hisp.dhis.common.FileTypeValueOptions" : { | ||
"type" : "string" | ||
} | ||
} | ||
} |