Skip to content

Invalid OpenAPI 3.1 spec due to incorrect type for value of default #2995

Closed
@martin-tarjanyi

Description

@martin-tarjanyi

Describe the bug

The python openapi-spec-validator library considers a spec invalid if the type of a property is different from the type of the value of default property.

To Reproduce

Spring Boot: 3.4.5
springdoc-openapi: 2.8.6

Actual Result:

{
    "openapi": "3.1.0",
    "info": {
        "title": "OpenAPI definition",
        "version": "v0"
    },
    "servers": [
        {
            "url": "http://localhost:8080",
            "description": "Generated server url"
        }
    ],
    "tags": [
        {
            "name": "Todo",
            "description": "Todo task management endpoints"
        }
    ],
    "paths": {
       // ...
     },
    "components": {
        "schemas": {
            "TodoResponse": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "string"
                    },
                    "title": {
                        "type": "string"
                    },
                    "completed": {
                        "type": "boolean",
                        "default": "true"  // <---------
                    }
                },
                "required": [
                    "id",
                    "title"
                ]
            }
        }
    }
}

Expected Result:

{
    "openapi": "3.1.0",
    "info": {
        "title": "OpenAPI definition",
        "version": "v0"
    },
    "servers": [
        {
            "url": "http://localhost:8080",
            "description": "Generated server url"
        }
    ],
    "tags": [
        {
            "name": "Todo",
            "description": "Todo task management endpoints"
        }
    ],
    "paths": {
       // ...
     },
    "components": {
        "schemas": {
            "TodoResponse": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "string"
                    },
                    "title": {
                        "type": "string"
                    },
                    "completed": {
                        "type": "boolean",
                        "default": true  // <---------
                    }
                },
                "required": [
                    "id",
                    "title"
                ]
            }
        }
    }
}

Example controller:

@RestController
@Tag(name = "Todo", description = "Todo task management endpoints")
class TodoController {
    @GetMapping("/todos/{id}")
    suspend fun getTodo(@PathVariable("id") id: String): TodoResponse =
      TodoResponse(
        id = id,
        title = "Sample Todo",
        completed = false,
      )
}

data class TodoResponse(
    val id: String,
    val title: String,
    @Schema(defaultValue = "true")
    val completed: Boolean,
)

Expected behavior

default value should have the same type as the type of the property

Additional context
Although, unlike 3.0 the 3.1 spec doesn't enforce this rule, it is still strongly recommended to follow it:
https://json-schema.org/draft/2020-12/json-schema-validation#name-default

This keyword can be used to supply a default JSON value associated with a particular schema. It is RECOMMENDED that a default value be valid against the associated schema.

This works correctly when target openapi version is 3.0

springdoc.api-docs.version=openapi_3_0

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