-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
collapse-type-hierarchy="true" fixes issue with @JsonIgnoreProperties, but creates issue with json-client. #1114
Comments
Thanks for the report. When you suggest supporting |
There's a little more to it. There's 2 pieces to
* Note a field may support multiple views which it is a member of, eg In addition to the API classes, each endpoint may optionally also specify @GET
@Path("persons")
@JsonView(Person.Summary.class) // <---
public Response get(@BeanParam PersonsSearchRequest query) { is doing this: ObjectMapper mapper = new ObjectMapper();
String result = mapper
.writerWithView(Person.Summary.class)
.writeValueAsString(item); This also can be applied to requests (today I learned...) @POST
@Path("person/{id}")
public Response get(@PathParam("id") String id, @JsonView(Person.Create.class) Person person) { is doing this: ObjectMapper mapper = new ObjectMapper();
String result = mapper
.readerWithView(Person.Create.class)
.forType(Person.class)
.readValue(json); Baeldung explains it pretty well. So the other key is that each endpoint may specify the |
I've decided I'll have to take this on at the same time as #539 so I'm pulling this off the release plan for now. |
I'm at a bit of an impasse.
I have a situation where I want to use
collapse-type-hierarchy="true"
when generating the docs, but not when generating the java json client. This is related to what #539 is requesting support for.I have a model structure that's highly relational, and sometimes cyclical eg:
When requesting a single person from my endpoint, we use
@JsonView
to restrict how deep the information being returned will go. The models are JPA entities. Without some restrictions in place via@JsonView
, when serializing the models they would just keep fetching and fetching until it has reached the end of the relationship line, and the whole kitchen sink is returned, eg:Through
@JsonView
, I can achieve shortening the depth of the API response.Note that the following fields are returned:
And the following fields are not returned because they have
@JsonView
specified on the field whose value (the class(es) specified in the annotation) does not match or is not a superclass of@JsonView(People.Detailed.class)
, which is specified on the endpoint.The
jobs[].location
field is marked with@JsonView(Job.Detailed.class)
which is not an ancestor of the requested view@JsonView(People.Detailed.class)
.The
supervisor.jobs[]
field is marked with@JsonView(Supervisor.Detailed.class)
which is not an ancestor of the requested view@JsonView(People.Detailed.class)
. (I didn't actually write the Supervisor class in the example)Now, enunciate doesn't support the
@JsonView
, so what I have done is used@TypeHint
and created classes specifically for the API docs to more accurately describe what's actually being returned from the API when generating the docs and the docs examples.Then, I can tell the docs to use this model in the API docs as the response so that the API docs shows it properly:
PersonDetailed extends Person so that it may inherit any future properties, and so that I don't have to duplicate all the fields to create this representation of what's actually being returned. This model is solely for correcting the API docs.
Here's another example, where I'd just want the summary view of the person since I'm returning a list of people.
Lastly, in order for the docs to properly omit the
"jobs", "supervisor"
fields and use the @typehints described on thePerson.PersonSummary.class
when generating the example json in the api docs for both the endpoint, and thePersonSummary
model, I have to configure:If I don't configure this, then the docs show that PersonSummary extends PersonDetailed extends Person. While it's great to have that info, without collapsing the type hierarchy, the fields I want to mask in the generated docs by using
@TypeHint
and the fields I want to ignore by@JsonIgnoreProperties
seem to be ignored/overwritten by the fact that those fields exist differently in the superclasses, and those superclass fields are taking precedence when generating the example json. Hard to say if that's a bug or a feature.^^^ All of this works great. I can perfectly describe my API
@JsonView
response behavior in the generated docs + html.However, now when I set
collapse-type-hierarchy="true"
, it is affecting my enunciate generated java-json-client artifact (and probably other generated clients? but I don't use those). I make use of dynamic types with@JsonTypeInfo
and@JsonSubTypes
, which requires not flattening the type hierarchy to function properly.So, now I'm at an impasse. I can either use
collapse-type-hierarchy="false"
and have the docs generate incorrectly (and it takes much longer to generate too when it's generating huuuuuge and often useless example json), or havecollapse-type-hierarchy="true"
and my java-json-client artifacts now don't support deserializing models where we use the@JsonTypeInfo
feature. I could go into more detail on this if needed.My attempts at working around this:
I was attempting having two separate maven plugin declarations - one for generating the docs pages using
collapse-type-hierarchy="true"
and another for the java-json-client withcollapse-type-hierarchy="false"
, but it seems as though they share the build working directory and config file, and only do the steps in the<jackson>
module once.I was then going the route of trying to split the api generation into two separate maven modules. While I think that's possible, it's really hacky feeling because it involves generating and exporting the sources of my war module for use by the api docs modules, then since the docs generation is being done without the java-json-client, I also must manually include the artifact as a download. Not sure what other problems I'd run into.
Suggestions for a fix:
@TypeHint
and@JsonIgnoreProperties
are ignored when displaying the example generated json on the docs pages when extending a class.@JsonView
(not holding my breath because that sounds like it could be a lot of work with not a lot of demand)Any other ideas?
The text was updated successfully, but these errors were encountered: