DEV Community

Cover image for Still on Swagger 2.0? Catch breaking changes without migrating first
Graphwake
Graphwake

Posted on Originally published at graphwake.com

Still on Swagger 2.0? Catch breaking changes without migrating first

Somewhere in your company there is a service that still publishes Swagger 2.0. Maybe it's an ASP.NET Core API that set Swashbuckle to 2.0 years ago for an old client. Maybe it's a Spring service on Springfox, or a Go service documented with swag. It works, nobody wants to touch it, and moving it to OpenAPI 3 sits somewhere near the bottom of the backlog.

That's fine. A 2.0 spec describes the API just as well, and you can catch the changes in it that break other teams without migrating anything first.

We build Graphwake, which tells the teams that consume an API when a change in its spec is about to break them. Graphwake doesn't read your code; it works only from the spec and from the endpoints each team subscribes to. Since this week it takes Swagger 2.0 as it is. This post covers where breaking changes hide in a 2.0 spec, how to get the file out of your build, and what you see once it's connected.

Why so many services are still on 2.0

Usually nobody chose it. The generator did:

  • Swashbuckle (ASP.NET Core) writes OpenAPI 3.0 by default, but many projects set it to 2.0 (SerializeAsV2 in older versions, OpenApiVersion today) to keep an older client or gateway happy, and never switched back.
  • Springfox (Spring) serves Swagger 2.0 at /v2/api-docs with @EnableSwagger2.
  • swag (Go) writes Swagger 2.0 to docs/swagger.json.

The usual workaround is a conversion step in the pipeline: turn the 2.0 file into OpenAPI 3, keep the converted copy somewhere, compare that. It's one more script to own, and it has a trap. If the old and the new spec go through different converters, or different versions of the same one, the diff reports changes that come from the converters, not from your API.

Where a breaking change hides in a 2.0 spec

A breaking change means the same thing in any version: after it, a client built against the old spec can fail. In a 2.0 spec it sits in a few specific places:

  • The body parameter. A request body is a parameter with in: body. A new required property in its schema breaks every client that doesn't send it.
  • definitions. Shared schemas live here. Remove or retype one property and every operation that references the schema changes with it, so one line can break ten endpoints.
  • produces and consumes. Drop a media type a client sends or asks for, and that client breaks.
  • host, basePath and schemes. Together they make the base URL. A new basePath moves every endpoint while paths looks untouched.
# Before
definitions:
  Order:
    type: object
    required: [id]
    properties:
      id:
        type: string
      total:
        type: number
---
# After: total is gone from every response that returns an Order
definitions:
  Order:
    type: object
    required: [id]
    properties:
      id:
        type: string
Enter fullscreen mode Exit fullscreen mode

That second snippet looks like a small cleanup in review. Every consumer that reads total sees it differently.

Get the 2.0 file out of your build

Breaking change alerts need the spec on every build, as a file or a URL. The commands come from each project's own docs; file names and ports are examples.

Swashbuckle, written at build time with its CLI:

dotnet tool install -g Swashbuckle.AspNetCore.Cli
swagger tofile --openapiversion 2.0 --output swagger.json bin/Release/net8.0/MyApi.dll v1
Enter fullscreen mode Exit fullscreen mode

Springfox serves the document while the app runs, so start the app in the job and read the URL:

curl -sf http://localhost:8080/v2/api-docs -o swagger.json
Enter fullscreen mode Exit fullscreen mode

swag:

go install github.com/swaggo/swag/cmd/swag@latest
swag init
# writes docs/swagger.json and docs/swagger.yaml
Enter fullscreen mode Exit fullscreen mode

Send it to Graphwake as it is

Graphwake watches OpenAPI 3.x and Swagger 2.0 specs. Swagger 2.0 is converted to OpenAPI 3.0 when it arrives; nothing to do on your side. No conversion step, no second file, and it's the same on every plan.

On GitHub Actions, point the step at the file your build wrote:

on:
  pull_request:
    types: [opened, synchronize, reopened, closed]
  push:
    branches: [main]

jobs:
  api-spec:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      # build the service; swag init writes docs/swagger.json
      - uses: graphwake/openapi-breaking-change-alerts@v1
        with:
          spec: docs/swagger.json
          key: ${{ secrets.GRAPHWAKE_KEY }}
Enter fullscreen mode Exit fullscreen mode

spec also takes a URL, so a Springfox job can give http://localhost:8080/v2/api-docs straight away. GitLab CI, Azure DevOps, Bitbucket Pipelines and Jenkins run the same open-source agent. If the spec is published somewhere, you can skip CI entirely: give Graphwake the URL and it checks it every 30 minutes.

From there a 2.0 service behaves like any other:

  • On a pull request that breaks something, the teams subscribed to the changed endpoint hear about it in Slack, Microsoft Teams or email, before the merge, with an impact score from 0 to 100. Teams that don't call it stay quiet.
  • On GitHub and Azure DevOps the pull request gets a comment with the breaking changes and the teams they affect.
  • The service page says "Converted from Swagger 2.0", so nobody wonders why the dashboard diff shows OpenAPI 3.0.

Example Graphwake alert in Slack for pull request #1284 on orders-api: a breaking change to GET /orders/{id}, owned by payments-team, where total was removed and currency is now required. It reaches the three subscribed teams (checkout, mobile-app, reporting); search is not subscribed and stays quiet. The change scores 79 out of 100, High.

Example: a breaking change, sent only to the teams that call the endpoint. A Swagger 2.0 service gets exactly the same alert.

The day you do migrate

When the service finally moves to a native OpenAPI 3 spec (Springfox to springdoc, or Swashbuckle back to its 3.0 default), the first diff compares a converted 2.0 spec with one written as 3.x. Some of what it reports comes from the format: a 2.0 spec without consumes becomes a */* request body, and the new generator writes application/json.

Graphwake flags that pull request. The alert and the comment say: "This spec moved from Swagger 2.0 to OpenAPI 3. Some of these changes may come from the format change, not from the API." Nothing is muted, though. Real breaking changes can hide in a format switch, so every one is still sent. Our advice: make the switch its own pull request, with no API change in it.

Two limits worth knowing. A spec that points to other files with external $refs has to be bundled into one file first. And the dashboard diff shows the converted OpenAPI 3.0 text, not your original 2.0 file.

To look at a single change first, paste the old and the new spec into the free OpenAPI diff. It takes Swagger 2.0 on either side and runs in your browser.


How many of your services still publish Swagger 2.0, and is moving them to OpenAPI 3 actually on anyone's roadmap?


About Graphwake. We build Graphwake, a dependency map for your APIs. It watches your OpenAPI specs, knows which teams consume each endpoint, and tells only those teams in Slack, Microsoft Teams or email when a change is about to break them. Start a 7-day free trial at graphwake.com

Top comments (0)