swagger-api / swagger-api/swagger-core

Name collision in api spec possible for colliding supertype names

Open
#4,706 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

backlog
Dominant language
Java
Stars
7.5k
Forks
2.3k
Avg merge
18h 1m
Merged PRs (30d)
10

Description

In a project where we use polymorphism quite a lot we identified that supertypes with a shared name might collide. The api spec output is therefore incorrect.
Instead of producing an incorrect swagger spec there should be an error raised.

The issue arises when there exist two (or more) type hierarchies with a common supertype name and the supertypes are annotated with @JsonSubTypes. The resulting model uses the allOf construct to specify the inherited properties. Where the schema is wrong is that only one of the supertype definitions is included. Subtypes of the other (not included) supertype reference the incorrect spec by name, while still referencing the supposed inherited properties (e.g. in the 'required' list).

For example, the supertype and subtype mismatch in the schema can look like this, Foo extends from the CollidingBase shown, but Bar has actually a different CollidingBase supertype:

  "components": {
    "schemas": {
      "CollidingBase": {
        "required": [
          "fooBase"
        ],
        "type": "object",
        "properties": {
          "fooBase": {
            "type": "string"
          }
        }
      },
      "Bar": {
        "required": [
          "bar",
          "barBase"
        ],
        "type": "object",
        "allOf": [
          {
            "$ref": "#/components/schemas/CollidingBase"
          },
          {
            "type": "object",
            "properties": {
              "bar": {
                "type": "string"
              }
            }
          }
        ]
      },

Versions:
io.swagger.core.v3 » swagger-core-jakarta 2.2.21 via:
org.springdoc:springdoc-openapi-starter-webmvc-ui version 2.5.0
org.springframework.boot version "3.3.0"

Example repository here: https://github.com/tbvh/springdoc-collision-demo (the full spec output is included in the repository)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the collision using the example repository linked in the issue and inspect its generated OpenAPI spec, especially the conflicting CollidingBase references and allOf sections. The work is done when swagger-core raises an error for colliding supertype names instead of emitting an incorrect specification.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.