fabiomaffioletti / jsondoc

Easily generate docs and playground for your RESTful API
http://jsondoc.org
MIT License
264 stars 127 forks source link

JsonDoc ignores transient modifier (leading to StackOverflowError) #228

Open joffrey-bion opened 7 years ago

joffrey-bion commented 7 years ago

In my project, I have a few classes that together make a dependency cycle. This cycle does not cause any trouble during serialization because I marked some fields as transient to break the cycle.

JsonDoc, however, ignores the transient modifier when creating object templates in JSONDocTemplateBuilder, and goes into infinite recursion up to the traditional StackOverflowError.

I could fix the issue by adding the following code in getDeclaredAllFields():

    private static Set<JSONDocFieldWrapper> getAllDeclaredFields(Class<?> clazz) {
        // ...

        for (Field field : declaredFields) {
            if (!shouldBeInTemplate(field)) {
                continue;
            }
           // ...
        }
        // ...
    }

    private static boolean shouldBeInTemplate(Field field) {
        return !field.isSynthetic() && !Modifier.isTransient(field.getModifiers());
    }

Similarly, in AbstractSpringJSONDocScanner.appendSubCandidates(), all fields are recursively explored and added to the candidates even if they are marked transient and thus will not be output by the API. It can easily be fixed by a similar check in the loop on the fields.

I can make a PR if you want to save time.