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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Documentation Debt Is the Real Risk in Long-Lived Network Infrastructure
  • Build Software Faster With Three Simple Principles
  • When Your Documentation Manages Itself: mdship and AI-Assisted Markdown
  • The Documentation Crisis Nobody Sees: Why AI Agents Are Breaking Faster Than Humans Can Document Them

Trending

  • Building a Feature Store With Neo4j and Feast
  • Microsoft Foundry Agent Debugging Dashboard with Microsoft Fabric Eventhouse, KQL, Docker and Grafana
  • Chaos Engineering and Kafka In Action: Topic Level Idempotence and Retry Check
  • Gitember: Building a Git Client Beyond Commit, Push, and Pull

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

Keeping OpenAPI DRY

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

By 
John Vester
DZone Core CORE ·
Feb. 21, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
55.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Documentation Debt Is the Real Risk in Long-Lived Network Infrastructure
  • Build Software Faster With Three Simple Principles
  • When Your Documentation Manages Itself: mdship and AI-Assisted Markdown
  • The Documentation Crisis Nobody Sees: Why AI Agents Are Breaking Faster Than Humans Can Document Them

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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