Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
martin-tarjanyi opened this issue May 14, 2025 · 1 comment
Open

Comments

@martin-tarjanyi
Copy link
Contributor

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
@Mattias-Sehlstedt
Copy link

Mattias-Sehlstedt commented May 14, 2025

Hi @martin-tarjanyi,

When I use 2.8.6 with a Java class, I also see the "true" representation rather than true. But if I bump the version to 2.8.7 it generates the expected true. Could you check if upgrading to 2.8.7 (or latest 2.8.8) resolves this issue for you?

I used

ResolvedSchema resolvedSchema = ModelConverters.getInstance(true)
                .resolveAsResolvedSchema(
                        new AnnotatedType(TodoResponse.class).resolveAsRef(false));

record TodoResponse(@Schema(defaultValue = "true") boolean bool) {}

and inspected the change in the default value in resolvedSchema.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants