DEV Community

Cover image for 1 in 5 examples in GitHub's OpenAPI description don't match their own schema
Sunghoon Jo
Sunghoon Jo

Posted on AI-assisted

1 in 5 examples in GitHub's OpenAPI description don't match their own schema

I'm building a library that checks, in tests, whether API responses match an OpenAPI document. Before trusting my validator I wanted to run it on large real-world specs, so I took four public ones (five documents, since GitHub publishes two versions) and checked every example in each document against the schema next to it:

I went looking for bugs in my library. I found more in the specs.

GitHub: 219 of 1,113 examples don't match

219 of the 1,113 response examples in GitHub's description (19.7%) fail the schema they sit next to. Counting each example once, by its first problem:

  • 136 are missing a field the schema marks as required.
  • 45 are just null, where the schema describes an object.
  • 13 match more than one branch of a oneOf, which has to match exactly one.
  • The other 25 have a value of the wrong type somewhere, such as a number where the schema expects a string.

Two of them come from endpoints you have probably called. GET /orgs/{org} returns an organization-full, whose schema lists archived_at (a nullable date-time) as required. The example has 66 fields, and archived_at isn't one of them.

GET /meta documents ssh_key_fingerprints as an object of strings. The example has numbers:

"ssh_key_fingerprints": {
  "SHA256_RSA": 1234567890,
  "SHA256_DSA": 1234567890,
  "SHA256_ECDSA": 1234567890,
  "SHA256_ED25519": 1234567890
}
Enter fullscreen mode Exit fullscreen mode

My first suspicion was my own validator. So I checked the same examples with an independent JSON Schema validator, @hyperjump/json-schema. It rejected 218 of the 219 as well. The remaining one has "developer.github.com" in a page_url field with format: uri; hyperjump doesn't check formats by default, which explains the difference.

I've reported the full list, with the first error for each example, in github/rest-api-description#7608.

The 3.1 version fails 13 more

GitHub publishes the same API as an OpenAPI 3.0 description and a 3.1 one. In the 3.1 version, 232 of the same examples fail. The extra 13 come from the conversion:

  • nullable: true, allOf: [$ref] in 3.0 became allOf: [$ref], type: ["null"] in 3.1. No value can pass that schema: null fails the $ref, and anything else fails the type.
  • nullable: true, oneOf: [...] became a type list with "null" in it, but the oneOf has no null branch, so null is rejected.

Moving nullable from 3.0 to 3.1 is trickier than it looks. If you convert your spec with a tool, it's worth validating the result.

Strict mode: fields the docs don't mention

With strict mode, which fails on properties the schema doesn't document, 43 GitHub examples have undocumented fields, such as template_repository. Stripe's spec has no examples, so I checked the fixtures Stripe publishes alongside it instead, and they too carry fields like charge.source that the schema doesn't list. Real responses having more fields than the docs is common, which is why strict mode is an option in my library rather than the default.

OpenAI and DigitalOcean

  • OpenAI: two path keys contain a query string, like /responses?beta=true. A path template can't have a query, so a request to /responses matches a different key. The four error examples (429 and 503) under those two keys can't be reached by any real request path.
  • DigitalOcean: all 968 operations are written as get: { $ref: resources/....yml }. OpenAPI allows a $ref for a Path Item, but not for an Operation. Tools like Redocly resolve it anyway, which is probably why nobody has noticed.

Why it matters

Spec examples show up as-is on documentation sites, and mock servers and SDK generators use them as input. When an example doesn't match its schema, a client written from the docs can disagree with the real responses or with the generated types.

The flip side is that this is easy to catch in a test: check each example in the spec against its schema. If you already have tests that compare responses with your spec, point the same validator at your examples too.

What I used

The library is openapi-matchers, Jest and Vitest matchers I wrote as a successor to jest-openapi, which is no longer maintained. Each spec is pinned to a commit and a SHA-256, and every failure is listed with its reason in test/real-specs/known-issues.ts, so you can reproduce all of this yourself.

If this is useful, a star on GitHub helps others find it.

Top comments (0)