fabiomaffioletti / jsondoc

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

Show All Objects that have annotation @ApiObject? #223

Open cnfn opened 7 years ago

cnfn commented 7 years ago

Now, JSONDoc only care that the objects with annotation ApiPathParam, ApiQueryParam, ApiBodyObject, ApiResponseObject.

If my custom object not use those annotations, I will not get JSONDoc for it.

In my team, HTTP API's ApiResponseObject is:

public class HttpResult {
    private String msg;
    private Integer code;
    private Object data;

    // getter and setter
}

JSONDoc not support my custom object by HttpResult.data property.

So I had modfied JSONDoc to support all objects that have annotation ApiObject, and use it in my team.

Would I can commit it?

ST-DDT commented 7 years ago

The @APIObjects will only be detected if they are used as a return type of a controller method.

See AbstractSpringJSONDocScanner#jsondocObjects()

But I agree that detecting more types would be nice. However I'm not sure whether it should support ALL of the annotated things. Maybe it would be a good idea to add a flag for this (detectUsedObjects, detectAnnotatedObjects). As an alternative I would suggest a new annotation. @APIDynamicType with a Class<?>[]value.

public class HttpResult {
    private String msg;
    private Integer code;

    @APIDynamicType({FooBar.class, AnyBar.class, FourtyTwo.class, MyDataContainer.class})
    private Object data;
}

This would allow the user to see what can actually be in there instead of just *Any*.

cnfn commented 7 years ago

@ST-DDT @APIDynamicType is a very very good idea because it's more friendly for reader. And it would be more wonderful that JSONDoc support link them between API and Objects.

I have a question, why only small different between has or not has @ApiObject? So I want do something to enhance it...

joffrey-bion commented 7 years ago

I find it strange that even with the annotation, it does not appear in JsonDoc.

I mean, if the developer made the effort to put the annotation ApiObject (and didn't specify show=false), then I can't see any scenario in which he does not want to see that object in the doc. What is the purpose of blocking these?

cnfn commented 7 years ago

I also have this question.

ST-DDT commented 7 years ago

Example fix:

Add the following to AbstractSpringJSONDocScanner#jsondocObjects()

[...]
for (final Method method : methodsAnnotatedWith) {
    buildJSONDocObjectsCandidates(candidates, method.getReturnType(), method.getGenericReturnType(),
            this.reflections);
    final Integer requestBodyParameterIndex = JSONDocUtils.getIndexOfParameterWithAnnotation(method,
            RequestBody.class);
    if (requestBodyParameterIndex != -1) {
        candidates.addAll(
                buildJSONDocObjectsCandidates(candidates, method.getParameterTypes()[requestBodyParameterIndex],
                        method.getGenericParameterTypes()[requestBodyParameterIndex], this.reflections));
    }
}

// NEWLY ADDED - START
candidates.addAll(this.reflections.getTypesAnnotatedWith(ApiObject.class, true));
// NEWLY ADDED - END

// This is to get objects' fields that are not returned nor part of the
// body request of a method, but that are a field
// of an object returned or a body of a request of a method
for (final Class<?> clazz : candidates) {
    appendSubCandidates(clazz, subCandidates, this.reflections);
}
[...]