r/SpringBoot • • Sep 09 '24

Too many annotations Swagger

I'm working on a Spring Boot project and using Swagger for API documentation. For my DTOs, I've been adding a lot of @Schema annotations to define the API schema properly, but it feels like it's really polluting my code and making the DTOs harder to read.

Is this a common issue, or am I overusing annotations? Are there better practices for integrating Swagger without cluttering the code too much? Any suggestions or alternatives would be appreciated!

25 Upvotes

17 comments sorted by

6

u/jollyboss123 Sep 09 '24

To be fair, you can move the annotations to an interface and have the controller or dto class inherit it, that way the clutter is moved elsewhere

1

u/BluePillOverRedPill Sep 09 '24

Hmm could you illustrate by an example?

4

u/jollyboss123 Sep 09 '24

2

u/BikingSquirrel Sep 09 '24

Nice, just going down that rabbit hole and had already seen the 'own annotation' idea. Having an interface as an alternative sounds good, will try what I like better in the end.

3

u/Emergency-Director53 Sep 09 '24 edited Sep 09 '24

I am a bit confused.

The usual best practice is to have separate API model objects and application DTOs.

And then you can do request and response mapping between those two.

I think if you are using DTO itself for schema(technically this will work) - should be fine but I think it makes the APIs and application code a bit closely coupled.

I think the decision on whether to use DTOs directly or to add request/response model layer depends on how complex your application is.

3

u/BluePillOverRedPill Sep 09 '24

I have a response/request type with a good amount of fields. But that means that I could end up with 10+ @Schema annotations in the class.

1

u/BikingSquirrel Sep 09 '24

The fun starts when you also add example objects.

3

u/Asdas26 Sep 09 '24

I think you're mistaking DTOs for internal models/domain objects used inside your application. DTO is the separate API model object you mention.

14

u/mehmetozanguven Sep 09 '24

You can write your swagger documentation directly into the .yml file. Then frontend and your spring boot application can generate client and controllers using the yml file.(Contract based development)

But in this way you will duplicate your current response classes and controllers. (I think, it is worth it)

8

u/Amfinaut Sep 09 '24

I'd prefer this approach also, if you can do it this way. You still keep the code and the API definition coupled by generating the code from the .yml. Spring openapi-generator could probably handle this.

Though there might be some cases where you'd want to write the controllers/interfaces yourself if the generator cannot produce whatever you'd want it to?

2

u/No-Philosophy-1189 Sep 09 '24

Wouldn't that add more confusion? Which controller would the request reach. The one we created or the one spring created? If we have to make changes in the controller, do we have to make changes in bothe files?

1

u/asarathy Sep 09 '24

You generate interfaces. Then when the yml changes you need to fix the implementation to deal with those changes.

1

u/asarathy Sep 09 '24

This is the way

7

u/Simple-Ice-6800 Sep 09 '24

I've ended up creating my own annotations that are nothing more than holders/wrappers for all the open API annotations. Basically ended up with @MyPersonCreateDocumentation where that interface has nothing but other annotations in the file.

2

u/[deleted] Sep 11 '24

Yeah, pretty common to feel like Swagger annotations clutter your code, especially with DTOs. One way to reduce this is by using centralized configuration files for Swagger or OpenAPI where possible, instead of heavily annotating every field. You can also use groups in Swagger to split up large DTOs or create custom annotations to reduce redundancy. DocsBot might help in a different context by organizing documentation more effectively, though it’s more about managing responses and customer support.

1

u/wimdeblauwe Sep 12 '24

That is the main reason I use Spring REST docs instead.