Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locale.ENGLISH should set #951

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
39 changes: 39 additions & 0 deletions src/test/java/com/networknt/schema/LocaleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.SpecVersion.VersionFlag;
import com.networknt.schema.i18n.Locales;
import com.networknt.schema.serialization.JsonMapperFactory;

public class LocaleTest {
private JsonSchema getSchema(SchemaValidatorsConfig config) {
Expand Down Expand Up @@ -65,4 +67,41 @@ void executionContextLocale() throws JsonMappingException, JsonProcessingExcepti
assertEquals(1, messages.size());
assertEquals("$.foo: integer trovato, string atteso", messages.iterator().next().getMessage());
}

/**
* Issue 949.
* <p>
* Locale.ENGLISH should work despite Locale.getDefault setting.
*
* @throws JsonMappingException the exception
* @throws JsonProcessingException the exception
*/
@Test
void englishLocale() throws JsonMappingException, JsonProcessingException {
Locale locale = Locale.getDefault();
try {
Locale.setDefault(Locale.GERMAN);
String schema = "{\r\n"
+ " \"$schema\": \"http://json-schema.org/draft-07/schema#\",\r\n"
+ " \"$id\": \"https://www.example.com\",\r\n"
+ " \"type\": \"object\"\r\n"
+ "}";
JsonSchema jsonSchema = JsonSchemaFactory.getInstance(VersionFlag.V7)
.getSchema(JsonMapperFactory.getInstance().readTree(schema));
String input = "1";
Set<ValidationMessage> messages = jsonSchema.validate(input, InputFormat.JSON);
assertEquals(1, messages.size());
assertEquals("$: integer wurde gefunden, aber object erwartet", messages.iterator().next().toString());

SchemaValidatorsConfig config = new SchemaValidatorsConfig();
config.setLocale(Locale.ENGLISH);
jsonSchema = JsonSchemaFactory.getInstance(VersionFlag.V7)
.getSchema(JsonMapperFactory.getInstance().readTree(schema), config);
messages = jsonSchema.validate(input, InputFormat.JSON);
assertEquals(1, messages.size());
assertEquals("$: integer found, object expected", messages.iterator().next().toString());
} finally {
Locale.setDefault(locale);
}
}
}
Loading