From 15e4229d6d7687343a97b02983632ad973fbad49 Mon Sep 17 00:00:00 2001 From: Joe Littlejohn Date: Wed, 15 Feb 2023 22:38:29 +0000 Subject: [PATCH] Clear schema cache between JSON files When using JSON or YAML files (not schemas, but example data) the schemas will not have a proper file path in their ID, as there is no actual schema file. All schema IDs will be fragments only (relative references within the document). Once generation is complete for a file, these schemas should be thrown away and not reused. Closes #1427 --- .../org/jsonschema2pojo/Jsonschema2Pojo.java | 4 +++ .../org/jsonschema2pojo/SchemaMapper.java | 4 +++ .../integration/json/JsonTypesIT.java | 25 +++++++++++++++++++ .../resources/json/propertiesSameName/a.json | 5 ++++ .../resources/json/propertiesSameName/b.json | 5 ++++ 5 files changed, 43 insertions(+) create mode 100644 jsonschema2pojo-integration-tests/src/test/resources/json/propertiesSameName/a.json create mode 100644 jsonschema2pojo-integration-tests/src/test/resources/json/propertiesSameName/b.json diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Jsonschema2Pojo.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Jsonschema2Pojo.java index 1c87ea089..dac94a497 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Jsonschema2Pojo.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/Jsonschema2Pojo.java @@ -127,6 +127,10 @@ private static void generateRecursive(GenerationConfig config, SchemaMapper mapp for (File child : schemaFiles) { if (child.isFile()) { + if (config.getSourceType() == SourceType.JSON || config.getSourceType() == SourceType.YAML) { + // any cached schemas will have ids that are fragments, relative to the previous document (and shouldn't be reused) + mapper.getRuleFactory().getSchemaStore().clearCache(); + } mapper.generate(codeModel, getNodeName(child.toURI().toURL(), config), defaultString(packageName), child.toURI().toURL()); } else { generateRecursive(config, mapper, codeModel, childQualifiedName(packageName, child.getName()), Arrays.asList(child.listFiles(config.getFileFilter()))); diff --git a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/SchemaMapper.java b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/SchemaMapper.java index 03522363e..757ec9b3f 100644 --- a/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/SchemaMapper.java +++ b/jsonschema2pojo-core/src/main/java/org/jsonschema2pojo/SchemaMapper.java @@ -140,4 +140,8 @@ private ObjectMapper objectMapper() { .enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); } + public RuleFactory getRuleFactory() { + return ruleFactory; + } + } diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/json/JsonTypesIT.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/json/JsonTypesIT.java index 9f134fa65..14badd401 100644 --- a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/json/JsonTypesIT.java +++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/json/JsonTypesIT.java @@ -210,4 +210,29 @@ public void arrayAtRootProducesNoJavaTypes() throws Exception { } + @Test + @SuppressWarnings("rawtypes") + public void propertiesWithSameNameOnDifferentObjects() throws Exception { + + ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/json/propertiesSameName", "com.example", + config("sourceType", "json")); + + Class aType = resultsClassLoader.loadClass("com.example.A"); + Class bType = resultsClassLoader.loadClass("com.example.B"); + Class aFieldsType = resultsClassLoader.loadClass("com.example.Fields"); + Class bFieldsType = resultsClassLoader.loadClass("com.example.Fields__1"); + + Object deserialisedValueA = OBJECT_MAPPER.readValue(this.getClass().getResourceAsStream("/json/propertiesSameName/a.json"), aType); + Object deserialisedValueB = OBJECT_MAPPER.readValue(this.getClass().getResourceAsStream("/json/propertiesSameName/b.json"), bType); + + Object aFields = aType.getMethod("getFields").invoke(deserialisedValueA); + Object onlyOnA = aFieldsType.getMethod("getOnlyOnA").invoke(aFields); + assertThat(onlyOnA, is("aaa")); + + Object bFields = bType.getMethod("getFields").invoke(deserialisedValueB); + Object onlyOnB = bFieldsType.getMethod("getOnlyOnB").invoke(bFields); + assertThat(onlyOnB, is("bbb")); + + } + } diff --git a/jsonschema2pojo-integration-tests/src/test/resources/json/propertiesSameName/a.json b/jsonschema2pojo-integration-tests/src/test/resources/json/propertiesSameName/a.json new file mode 100644 index 000000000..9c4465a68 --- /dev/null +++ b/jsonschema2pojo-integration-tests/src/test/resources/json/propertiesSameName/a.json @@ -0,0 +1,5 @@ +{ + "fields": { + "onlyOnA": "aaa" + } +} diff --git a/jsonschema2pojo-integration-tests/src/test/resources/json/propertiesSameName/b.json b/jsonschema2pojo-integration-tests/src/test/resources/json/propertiesSameName/b.json new file mode 100644 index 000000000..be83c8916 --- /dev/null +++ b/jsonschema2pojo-integration-tests/src/test/resources/json/propertiesSameName/b.json @@ -0,0 +1,5 @@ +{ + "fields": { + "onlyOnB": "bbb" + } +}