Skip to content

Commit

Permalink
Clear schema cache between JSON files
Browse files Browse the repository at this point in the history
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
  • Loading branch information
joelittlejohn committed Feb 16, 2023
1 parent ef5893b commit 15e4229
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,8 @@ private ObjectMapper objectMapper() {
.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
}

public RuleFactory getRuleFactory() {
return ruleFactory;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"fields": {
"onlyOnA": "aaa"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"fields": {
"onlyOnB": "bbb"
}
}

0 comments on commit 15e4229

Please sign in to comment.