springfox / springfox

Automated JSON API documentation for API's built with Spring
http://springfox.io
Apache License 2.0
5.93k stars 1.54k forks source link

Support for @JsonValue on a wrapper class #1871

Open ilya40umov opened 7 years ago

ilya40umov commented 7 years ago

I am facing the same problem as was described in: https://github.com/springfox/springfox/issues/184

Essentially, when you have a class that wraps around a list and uses @JsonValue to flatten the resulting JSON (so that it is "[{...}, {...}, ...]" instead of "{'elements':[{...}, {...}, ...]}"), you end up with an empty model and an empty [] in the example.

A model to reproduce this problem might look like this:

public class Products {

    private List<Product> products;

    @JsonValue
    public List<Product> getProducts() {
        return this.products;
    }
}

I was able to work around this problem by extending a ForwardingList from Guava, but in my opinion JsonValue annotation should also be supported.

dilipkrish commented 7 years ago

Would really appreciate a PR for this.

matdelong commented 7 years ago

This is a feature I require.

rgroothuijsen commented 7 years ago

What seems to be happening is that the Jackson parser explicitly declines to include JsonValue getters as regular getters, and as a result it concludes further down the line that the field is invisible because there's no getter. The slightly hacky solution is to manually insert the field afterwards, and it works at least somewhat for the purpose described in this issue. Jackson 2.9 had improvements for getting JsonValue fields, so in the future a cleaner solution might be possible.

I've written a piece of code that at the very least fetches the field, though the Swagger output unfortunately still contains the list field name. Not sure how to fix that.

dilipkrish commented 7 years ago

@rgroothuijsen thanks for posting an update. I'll take a look at your patch.

rgroothuijsen commented 7 years ago

I've been looking into it a bit further, and I haven't found a good way to cleanly generate anonymous JsonValue output in the Swagger spec because Swagger uses key-value maps to generate its property structure. Given that, it would come down to a choice between no output or bad output for JsonValue fields.