Skip to content

Redundant "type" property of subclass in class hierarchy #3454

@aldan95

Description

@aldan95

When created specification for polymorph class model structure there is unnecessary "type: object" property preceding "allOf" in all subclasses (Text and Video in following example):

    "schemas": {
      "Message": {
        "required": [
          "type"
        ],
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "Text",
              "Video"
            ]
          }
        },
        "discriminator": {
          "propertyName": "type",
          "mapping": {
            "Text": "#/components/schemas/Text",
            "Video": "#/components/schemas/Video"
          }
        }
      },
      "Text": {
        "required": [
          "text",
          "type"
        ],
        "type": "object",    # redundant
        "allOf": [
          {
            "$ref": "#/components/schemas/Message"
          },
          {
            "type": "object",
            "properties": {
              "text": {
                "type": "string"
              }
            }
          }
        ]
      },
      "Video": {
        "required": [
          "previewUrl",
          "type",
          "videoUrl"
        ],
        "type": "object",     # redundant
        "allOf": [
          {
            "$ref": "#/components/schemas/Message"
          },
          {
            "type": "object",
            "properties": {
              "videoUrl": {
                "type": "string"
              },
              "previewUrl": {
                "type": "string"
              },
              "text": {
                "type": "string"
              }
            }
          }
        ]
      }
    }

OpenApi spec contains no such property for subclasses.

This extra property leads to errors on api client generation with SwagGen tool.

Sample test controller used:

package test.org.springdoc.api.app5

import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo
import io.swagger.v3.oas.annotations.media.DiscriminatorMapping
import io.swagger.v3.oas.annotations.media.Schema
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

enum class MessageType {
	Text, Video
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes(
	JsonSubTypes.Type(value = Text::class, name = "Text")
	,JsonSubTypes.Type(value = Video::class, name = "Video")
)
@Schema(
	discriminatorMapping = [
		DiscriminatorMapping(value = "Text", schema = Text::class),
		DiscriminatorMapping(value = "Video", schema = Video::class)
	]
)
abstract class Message(val type: MessageType)

data class Text(
	val text: String
): Message(MessageType.Text)

data class Video(
	val videoUrl: String,
	val previewUrl: String,
	val text: String?
): Message(MessageType.Video)

@RestController
@RequestMapping("/test")
class HelloController {
	@GetMapping
	fun index(): Message? = null
}

moved from springdoc/springdoc-openapi#452

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions