DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Documentation Crisis Nobody Sees: Why AI Agents Are Breaking Faster Than Humans Can Document Them
  • The Developer's Guide to Context-Aware AI: When Your Code Documentation Becomes Intelligent
  • We Tested Context7 With ZK Documentation: Here's What We Learned
  • Master Developer Writing: From Docs and Pull Requests to Blog Posts

Trending

  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • DevOps and Platform Engineering Readiness Checklist: Everything Needed for a Scalable, Secure, High-Velocity Delivery Platform
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives

Keeping OpenAPI DRY

See an example of how your OpenAPI documentation can provide the necessary information without introducing duplication.

By 
John Vester user avatar
John Vester
DZone Core CORE ·
Feb. 21, 20 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
55.0K Views

Join the DZone community and get the full member experience.

Join For Free

I really enjoy API development. As someone who enjoys writing, I also enjoy providing solid documentation for consumers of my APIs to utilize. When my APIs are used by teams across the United States and other countries it becomes important to provide reliable and detailed information. The same is true for public APIs when the consumer is simply not known.

However, I really am not a fan of repetition in my code...and that includes API documentation.

When our team ran into an issue with SpringFox and SpringBoot version 2.2.x, I decided to convert from using SpringFox to springdoc-openapi for our API documentation. Having seen a lot of repeated items in the SpringFox annotations from one controller method to another, I decided to figure out how to employ the DRY (don't repeat yourself) principle to the OpenAPI documentation, which we would serve up using Swagger.

You may also like: Software Design Principles DRY and KISS

A Common Example

Consider the following controller example:

Java
 




x
15


 
1
@Operation(summary = "For a given accountId, return a list of orders.")
2
@ApiResponses(value = { 
3
  @ApiResponse(responseCode = "200", description = "ok"), 
4
  @ApiResponse(responseCode = "400", description = "request could not be processed"),
5
  @ApiResponse(responseCode = "401", description = "user account is not valid", content = @Content()),
6
  @ApiResponse(responseCode = "403", description = "user does not have access to perform this request", content = @Content())
7
})
8
@GetMapping(value = "account/{accountId}/orders")
9
public ResponseEntity<List<Order>> getOrders(@PathVariable Long accountId) {
10
    try {
11
        return new ResponseEntity<>(orderService.getOrders(accountId), HttpStatus.OK);
12
    } catch (OrderException e) {
13
       return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
14
    }
15
}



There are some annotations in use that drive the population of data for the Swagger UI.  

You may have noticed that the controller throws a BAD_REQUEST response, but Swagger accounts for UNAUTHORIZED and FORBIDDEN responses as well. These will be thrown from a Security Interceptor. If you want to see an example, simply review the following article I published last month:

Using @RequestScope With Your API

While the information in the example above will produce very nice documentation, after a couple of APIs, it will be easy to realize there is a lot of duplication of annotation text involved. This would be not so DRY, if you ask me.

There has to be a better way to do this.

Enter Annotations

Since Java version 5, developers have been able to create and use annotations to make their life easier. So, certainly there has to be a way to use an annotation to keep from repeating myself on every controller that I am exposing to Swagger.

Turns out, it was quite simple.  Consider the following example for the UNAUTHORIZED and FORBIDDEN documentation:

Java
 




xxxxxxxxxx
1


 
1
@Retention(RetentionPolicy.RUNTIME)
2
@Target(ElementType.METHOD)
3
@ApiResponses(value = {
4
  @ApiResponse(responseCode = Constants.UNAUTHORIZED, description = "request could not be processed", content = @Content()),
5
  @ApiResponse(responseCode = Constants.FORBIDDEN, description = "user account is not valid", content = @Content())
6
})
7
public @interface ApiAuthResponses { }



Now, I can simply include @ApiAuthResponses to my controller to include these two responses. What this means is that the content is in one location, so if we decide to change the text, the changes only have to be made one time.

Also, I went ahead and established constants for all of our basic responseCode and description values too:

Java
 




xxxxxxxxxx
1
17


 
1
public static final String OK = "200";
2
public static final String CREATED = "201";
3
public static final String ACCEPTED = "202";
4
public static final String NO_CONTENT = "204";
5
public static final String BAD_REQUEST = "400";
6
public static final String UNAUTHORIZED = "401";
7
public static final String FORBIDDEN = "403";
8
public static final String NOT_FOUND = "404";
9
public static final String METHOD_NOT_ALLOWED = "405";
10
public static final String CONFLICT = "409";
11
public static final String UNPROCESSABLE = "422";
12
public static final String INTERNAL_SERVER_ERROR = "500";
13

          
14
public static final String ACCEPTED_TEXT = "accepted";
15
public static final String CREATED_TEXT = "created";
16
public static final String NO_CONTENT_TEXT = "no content";
17
public static final String OK_TEXT = "ok";



The Updated Controller

With the interface and constants in place, the updated controller appears as follows:

Java
 




xxxxxxxxxx
1
14


 
1
@Operation(summary = "For a given accountId, return a list of orders.")
2
@ApiResponses(value = { 
3
  @ApiResponse(responseCode = Constants.OK, description = Constants.OK_TEXT), 
4
  @ApiResponse(responseCode = Constants.BAD_REQUEST, description = "request could not be processed")
5
})
6
@ApiAuthReponses
7
@GetMapping(value = "account/{accountId}/orders")
8
public ResponseEntity<List<Order>> getOrders(@PathVariable Long accountId) {
9
    try {
10
        return new ResponseEntity<>(orderService.getOrders(accountId), HttpStatus.OK);
11
    } catch (OrderException e) {
12
       return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
13
    }
14
}



Now, the documentation for the getOrders() URI contains only information that is applicable for this controller method. Common responseCodes and descriptions were moved to a Constants class. The documentation for common exceptions was pushed into the ApiAuthResponses class.

As a result, my OpenAPI documentation is exactly how I like it...useful and quite DRY. 

Have a really great day!

Further Reading

DevOps and the DRY Principle

Documentation

Opinions expressed by DZone contributors are their own.

Related

  • The Documentation Crisis Nobody Sees: Why AI Agents Are Breaking Faster Than Humans Can Document Them
  • The Developer's Guide to Context-Aware AI: When Your Code Documentation Becomes Intelligent
  • We Tested Context7 With ZK Documentation: Here's What We Learned
  • Master Developer Writing: From Docs and Pull Requests to Blog Posts

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook