ScaCap / spring-auto-restdocs

Spring Auto REST Docs is an extension to Spring REST Docs
https://scacap.github.io/spring-auto-restdocs/
Apache License 2.0
311 stars 86 forks source link

Support for @JsonView #379

Closed andue closed 10 months ago

andue commented 4 years ago

Are there any plans to support sth like here with spring auto restdocs?

at the moment, i don't think that @JsonView annotation will be considered.

if you could provide some feedback for support/contribution, that would be great. thx

jmisur commented 4 years ago

Hi Andreas, the current logic takes whatever is present in the response pojo and outputs as response body fields. We are using Jackson deserialiser for our advantage, however it has it's limitation. We're not supporting too advanced/niche features.

andue commented 4 years ago

thx Juraj for your feedback! so, @JsonView would be a feature you`d not like to support?

as said, i would be happy to contribute, but just had no time yet, to look for where to start...

jmisur commented 4 years ago

We would like to support it, however we're most likely not going to work on it ourselves. If you have time you could take a look of course, any work is appreciated. Let us know if you hit any problems. There is an additional challenge to this annotation as it's on the method, not the pojo itself, so some analysis and refactoring of the current traverse logic would be needed.

mkeith6911 commented 2 years ago

I ran into this myself recently when trying to use this in a project. While this likely wouldn't work as a solution for the project (ie: for everyone), this resolved the issue for me.

For my use case, I don't actually care to parse the classes to look at the class / field level @JsonView annotations. Instead, I only care if the response object, request object, or @ModelAttribute object is also annotated with a @JsonVIew.

I started by overriding the AbstractJacksonFieldSnippet class so that I could alter the createFieldDescriptors method. My goal was to alter how the object writer was built.

I first needed to detect what type of field snippet was being built, so I knew where to look for the relevant @JsonView annotation. This was simple enough because the superclass will tell you if it's a response, request, or model attribute snippet.

Once you determine where to look, all you have to do is actually look at the relevant portion of the handler and check to see if the relevant objects are annotated with @JsonView.

Request Field Snippet >> Check for any argument that is annotated with @RequestBody and also @JsonView. Response Field Snippet >> Check if the response object is annotated with @JsonView. Model Attribute Snippet >> Check for any argument that is annotated with @ModelAttribute and also @JsonView.

If this is not found, I simply use the same object writer that was already being constructed. However, if the annotation is detected, I construct the object writer with: objectMapper.writerWithView(annotation.value()[0])

Yes, this will pull the first view when the relevant object is annotated with multiples. That was beyond the scope of my use-case.

Figured I'd share in case anyone else has the same use-case and would also like to benefit from using this project to help automatically generate documentation.