openapi-generator-for-spring icon indicating copy to clipboard operation
openapi-generator-for-spring copied to clipboard

Improve numberOfUsages calculation

Open neiser opened this issue 4 years ago • 0 comments

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.

neiser avatar Nov 30 '20 10:11 neiser