De-duplicate identical classes and avoid __1#1655
De-duplicate identical classes and avoid __1#1655matusfaro wants to merge 11 commits intojoelittlejohn:masterfrom
__1#1655Conversation
| private GenerationConfig generationConfig; | ||
| private Annotator annotator; | ||
| private SchemaStore schemaStore; | ||
| private Map<Class<?>, Map<String, ?>> dedupeCache; |
There was a problem hiding this comment.
Track duplicates in each RuleFactory instance
| return new ObjectRule(this, new ParcelableHelper(), reflectionHelper); | ||
| Rule<JPackage, JType> rule = new ObjectRule(this, new ParcelableHelper(), reflectionHelper); | ||
| if (generationConfig.isUseDeduplication()) { | ||
| rule = new DeduplicateRule<>(dedupeCache, rule); |
There was a problem hiding this comment.
Wrap specific Rules that require de-duplication.
|
|
||
| public DeduplicateRule(Map<Class<?>, Map<String, ?>> dedupeCacheByRule, Rule<T, R> rule) { | ||
| // noinspection unchecked map is populated by us and guaranteed to be of the correct type | ||
| Map<String, R> dedupeCache = (Map<String, R>) dedupeCacheByRule.get(rule.getClass()); |
There was a problem hiding this comment.
Each Rule has its own individual cache
| * @return same {@code R} instance if a previous schema with the same hash code was already processed | ||
| */ | ||
| @Override | ||
| public R apply(String nodeName, JsonNode node, JsonNode parent, T generatableType, Schema currentSchema) { |
There was a problem hiding this comment.
Performs de-duplication here during rule transformation of Schema to result.
| return javaType != null; | ||
| } | ||
|
|
||
| public String calculateHash() { |
There was a problem hiding this comment.
Using SHA-256 of the compact JSON string for de-duplication.
|
Interesting, it deduplicated MenuItem, but not Menu by reference, it's possible I missed deduplicating references. I'll take a look later when I have time. In the meantime could you try giving a "title":"Menu" in Menu.json? |
|
@ahasgx I added additional tests across various json schemas and it is all working fine. Couple of notes:
|
|
Sorry, I missed the important property |
|
works like a charm! any news on this PR ? will it be merged soon ? thanks |
|
The owner @joelittlejohn is likely taking a well deserved break. (hasn't been active in this repo for last 7 months) For now you can use my fork and if this gets merged you can switch back to it later. |

Hello,
Thank you very much for your amazing project!
I know you have previously decided (in #1082) not to support de-duplication of identical classes and instead suggest to use
$refs, but please re-consider.This seems to be an arguably common request: #1562 #1559 #1151 #1192 #1081 #784 #114 #112 #885
Mainly this will solve the case where it's not possible to modify the input and replace
$refs (my case), but it'll also make it friendlier for others not having to refactor their input to use$refs.Take a look at the implementation which is fairly simple. I'd appreciate any feedback on the implementation, even if you don't decide to go ahead with it.
If I still can't change your mind, no problem, I will maintain this change in my fork.
UPDATE: For now I have it in my repo published under my groupId if anyone wants to use it, e.g.:
Make sure to use the new property
--useDeduplication