BBackend CourseLeroy Labs
0/0
Search the course

API documentation (OpenAPI) & versioning

springdoc-openapi and Swagger UI, documenting security, versioning strategies (/api/v1 vs headers), and evolving APIs without breaking clients.

● beginnerSpring-Boot-StoreHostel-Web-AppSperoTek (in dev)

OpenAPI

OpenAPI (formerly Swagger) is a machine-readable description of your API: paths, parameters, schemas, auth and responses. springdoc-openapi generates it at runtime from your controllers and DTOs:

  • /v3/api-docs serves the JSON spec.
  • /swagger-ui.html serves an interactive UI.
@Tag(name = "Bookings")
@Operation(summary = "Approve a pending booking")
@ApiResponse(responseCode = "409", description = "Room already full")
@PostMapping("/{id}/approve")
public BookingDto approve(@PathVariable UUID id) { /* ... */ }

@Bean
OpenAPI api() {
    return new OpenAPI()
        .info(new Info().title("Hostel API").version("v1"))
        .components(new Components().addSecuritySchemes("bearer",
            new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT")));
}

The spec is also useful for generating clients, e.g. a TypeScript SDK for your React frontend with openapi-typescript, and for contract tests.

In your projects

Hostel-Web-Apppom.xml (springdoc-openapi 3.0.2) · @Tag on controllers

Swagger is permitAll in SecurityConfig so the frontend team can browse it.

Spring-Boot-Store@Tag(name = "Carts")

springdoc 3.0.2 with tags per controller.

SperoTek (in dev)shared/config/OpenApiConfig.java · identity/web/OpenApiHeaders

SperoTek documents its custom headers (X-Step-Up-Token, X-Correlation-Id) and serves Swagger UI publicly. Consider protecting or disabling it in production for a private SaaS, because it hands attackers a map of your API.

Versioning

Strategy Example Pros Cons
URI path (SperoTek) /api/v1/companies Obvious, easy to route and cache URLs change between versions
Header Accept: application/vnd.spero.v2+json Clean URLs Harder to test and cache
Query param ?version=2 Simple Messy
None + evolution Additive changes only Simplest Needs discipline

SperoTek uses /api/v1/.... The other projects use /api/... with no version, which is fine while you control the only client.

Non-breaking vs breaking changes

  • Safe: adding an optional field, a new endpoint, or a new enum value (only if clients tolerate unknown values).
  • Breaking: removing or renaming a field, changing a type, making an optional field required, changing a status code.

The robustness principle: be liberal in what you accept and conservative in what you send. Clients should ignore unknown fields. Jackson’s FAIL_ON_UNKNOWN_PROPERTIES=false is Spring Boot’s default.

Mobile clients make this real

IR3D and the Hostel Life mobile app (React Native) mean old app versions stay in the wild for months. You can’t force-update everyone, so breaking changes need a new version or a long deprecation window. Use the Deprecation and Sunset headers to announce them.

  • API-first vs code-first. Writing the OpenAPI spec first and generating server stubs is the alternative to springdoc’s code-first approach.
  • Contract testing (Pact, Spring Cloud Contract) checks that a consumer and provider agree.
  • Changelogs. RM even ships in-app release notes (release_notes, release_note_reads tables).

Check yourself

1. Which change is breaking?
2. Why is versioning more critical for IR3D than an internal admin panel?