Skip to content

@Schema inside other schema overrides original schema. #1870

@marubuild

Description

@marubuild

Describe the bug
I have original schema class A, and the other class B which includes some properties(fields) that type of A.
If I produce openapi specification documents, schema A is overrided with the last field of class B which is type of A.
I think it is somehow wrong because I intended to use class A, not define inside class B.

To Reproduce
Steps to reproduce the behavior:

  • What version of spring-boot you are using?
    Spring boot 2.6.2 + kotlin 1.7
  • What modules and versions of springdoc-openapi are you using?
    org.springdoc:springdoc-openapi-ui:1.6.11
    org.springdoc:springdoc-openapi-kotlin:1.6.11
  • What is the actual and the expected result using OpenAPI Description (yml or json)?
    original schema is not overrided.
  • Provide with a sample code (HelloController) or Test that reproduces the problem
@Schema(name = "schemaA")
data class A(
    @get:Schema(description = "a")
    val fieldA: B?,
    @get:Schema(description = "b")
    val fieldB: B?
)

@Schema(name = "schemaB", required = true, nullable = true)
data class B(
    @get:Schema(description = "file", required = true)
    val name: String,
    @get:Schema(description = "URL", required = true)
    val url: String,
)

produced openapi specification

{
  "schemaB": {
    "required": [
      "name",
      "url"
    ],
    "type": "object",
    "properties": {
      "name": {
        "type": "string",
        "description": "file"
      },
      "url": {
        "type": "string"
      }
    },
    "description": "b",
    "nullable": true
  },
  "schemaA": {
    "type": "object",
    "properties": {
      "fieldA": {
        "$ref": "#/components/schemas/schemaB"
      },
      "fieldB": {
        "$ref": "#/components/schemas/schemaB"
      }
    }
  }
}

Expected behavior

  • A clear and concise description of what you expected to happen.
  • What is the expected result using OpenAPI Description (yml or json)?

expected openapi specification

{
  "schemaB": {
    "required": [
      "name",
      "url"
    ],
    "type": "object",
    "properties": {
      "name": {
        "type": "string",
        "description": "file"
      },
      "url": {
        "type": "string"
      }
    },
    "description": "b",
    "nullable": true
  },
  "schemaA": {
    "type": "object",
    "properties": {
      "fieldA": {
        "description": "a",
        "$ref": "#/components/schemas/schemaB"
      },
      "fieldB": {
        "description": "b",
        "$ref": "#/components/schemas/schemaB"
      }
    }
  }
}

Screenshots
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.
I found same issue reported and some maintainer replied improper ways to cover with it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidThis doesn't seem right

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions