|
26 | 26 | import com.fasterxml.jackson.databind.JsonMappingException;
|
27 | 27 | import com.fasterxml.jackson.databind.JsonNode;
|
28 | 28 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 29 | +import com.networknt.schema.SpecVersion.VersionFlag; |
29 | 30 | import com.networknt.schema.i18n.Locales;
|
| 31 | +import com.networknt.schema.serialization.JsonMapperFactory; |
30 | 32 |
|
31 | 33 | public class LocaleTest {
|
32 | 34 | private JsonSchema getSchema(SchemaValidatorsConfig config) {
|
@@ -65,4 +67,41 @@ void executionContextLocale() throws JsonMappingException, JsonProcessingExcepti
|
65 | 67 | assertEquals(1, messages.size());
|
66 | 68 | assertEquals("$.foo: integer trovato, string atteso", messages.iterator().next().getMessage());
|
67 | 69 | }
|
| 70 | + |
| 71 | + /** |
| 72 | + * Issue 949. |
| 73 | + * <p> |
| 74 | + * Locale.ENGLISH should work despite Locale.getDefault setting. |
| 75 | + * |
| 76 | + * @throws JsonMappingException the exception |
| 77 | + * @throws JsonProcessingException the exception |
| 78 | + */ |
| 79 | + @Test |
| 80 | + void englishLocale() throws JsonMappingException, JsonProcessingException { |
| 81 | + Locale locale = Locale.getDefault(); |
| 82 | + try { |
| 83 | + Locale.setDefault(Locale.GERMAN); |
| 84 | + String schema = "{\r\n" |
| 85 | + + " \"$schema\": \"http://json-schema.org/draft-07/schema#\",\r\n" |
| 86 | + + " \"$id\": \"https://www.example.com\",\r\n" |
| 87 | + + " \"type\": \"object\"\r\n" |
| 88 | + + "}"; |
| 89 | + JsonSchema jsonSchema = JsonSchemaFactory.getInstance(VersionFlag.V7) |
| 90 | + .getSchema(JsonMapperFactory.getInstance().readTree(schema)); |
| 91 | + String input = "1"; |
| 92 | + Set<ValidationMessage> messages = jsonSchema.validate(input, InputFormat.JSON); |
| 93 | + assertEquals(1, messages.size()); |
| 94 | + assertEquals("$: integer wurde gefunden, aber object erwartet", messages.iterator().next().toString()); |
| 95 | + |
| 96 | + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); |
| 97 | + config.setLocale(Locale.ENGLISH); |
| 98 | + jsonSchema = JsonSchemaFactory.getInstance(VersionFlag.V7) |
| 99 | + .getSchema(JsonMapperFactory.getInstance().readTree(schema), config); |
| 100 | + messages = jsonSchema.validate(input, InputFormat.JSON); |
| 101 | + assertEquals(1, messages.size()); |
| 102 | + assertEquals("$: integer found, object expected", messages.iterator().next().toString()); |
| 103 | + } finally { |
| 104 | + Locale.setDefault(locale); |
| 105 | + } |
| 106 | + } |
68 | 107 | }
|
0 commit comments