Skip to content

[Bug]: Example value of number format is generated as string in Open API spec 3.1 and gives spectral lint error #5177

@gauravtgithub

Description

@gauravtgithub

Description of the problem/issue

When generating schema of a field of type number (e.g., @Schema(type = "number", example = "428.94")), it appears that Swagger Core interprets the type as a string. This results in the example being serialized as a string instead of a number.

For instance :

@Schema(
description = "amount" + "

Example: 428.94",
type = "number",
example = "428.94",
requiredMode = REQUIRED)
BigDecimal amount;

In the generated OpenAPI schema, the example becomes:

properties:
amount:
type: number
description: "amount

Example: 428.94"
example: "428.94"

instead of :

properties:
amount:
type: number
description: "amount

Example: 428.94"
example: 428.94

Affected Version

e.g. 2.2.43

  1. Create any DTO class with a number type field
    @Schema(
    description = "amount" + "

    Example: 428.94",
    type = "number",
    example = "428.94",
    requiredMode = REQUIRED)
    BigDecimal amount;

  2. Generate open api docs
    ./gradlew generateOpenApiDocs

  3. Check the resulting yaml. It will have schema like

type: object
properties:
amount:
type: number
description: "amount

Example: 428.94"
example: "428.94"

  1. Run spectral lint
    npm install -g @stoplight/spectral@6.5.0
    spectral lint --format=stylish --fail-severity=warn file.yaml

Spectral will report errors
error oas3-valid-media-example "amount" property type must be string
error oas3-valid-schema-example "example" property type must be number

Expected Behavior

Generated yaml file should have example value as number

properties:
amount:
type: number
description: "amount

Example: 428.94"
example: 428.94

Actual Behavior

Generated yaml file has example value as a string

properties:
amount:
type: number
description: "amount

Example: 428.94"
example: "428.94"

Logs / Stack Traces

error oas3-valid-media-example "amount" property type must be string
error oas3-valid-schema-example "example" property type must be number

Checklist

  • I have searched the existing issues and this is not a duplicate.
  • I have provided sufficient information for maintainers to reproduce the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions