Currently, the numberOfUsages parameter for ReferenceDeciderForType is calculated by the number of setters which are going to consume the referenced item. This is a good estimate, but in general not correct for any case: For example, consider a Schema which is multiply used by identical ApiResponses, but in turn this ApiResponse is also referenced itself. Then the actual usage of that schema is 1, as the Schema is only used by one ApiResponse, but the number of setters is not merged in AbstractReferencedItemStorage, as the referencing of ApiResponses happens later than the referencing of Schemas.
Have a look at AbstractReferencedItemStorage method for a starting idea:
@Nullable
private Object findSetterTarget(Consumer<T> setter) {
Field[] declaredFields = setter.getClass().getDeclaredFields();
if (declaredFields.length == 1) {
Field declaredField = declaredFields[0];
declaredField.setAccessible(true);
try {
return declaredField.get(setter);
} catch (IllegalAccessException e) {
// this should not happen, as we've made it accessible...
return null;
}
}
return null;
}
This is a hard issue, but you can learn a lot about how referencing of items is done internally in this library.
Currently, the
numberOfUsagesparameter forReferenceDeciderForTypeis calculated by the number of setters which are going to consume the referenced item. This is a good estimate, but in general not correct for any case: For example, consider aSchemawhich is multiply used by identicalApiResponses, but in turn thisApiResponseis also referenced itself. Then the actual usage of that schema is 1, as the Schema is only used by one ApiResponse, but the number of setters is not merged inAbstractReferencedItemStorage, as the referencing of ApiResponses happens later than the referencing of Schemas.Have a look at
AbstractReferencedItemStoragemethod for a starting idea:This is a hard issue, but you can learn a lot about how referencing of items is done internally in this library.