Skip to content

Optional (nullable) enums fail JSON validation when explicitly set to "null" #239

@dmlambea

Description

@dmlambea

Describe the bug
A not required, nullable enum field fails JSON validation whenever is sent with explicit "null" value. If not sent at all, it passes validation.

To Reproduce
Run a server with: https://go.dev/play/p/0JQGk_sT-Ox?v=goprev
Then, try the code using cURL as specified below.

Expected behavior
Sending a request with the enum field set to null should validate correctly.

Additional context
My go.mod is:

module rest_example

go 1.23.1

require (
        github.com/go-chi/chi/v5 v5.2.3
        github.com/swaggest/rest v0.2.75
        github.com/swaggest/usecase v1.3.1
)

require (
        github.com/cespare/xxhash/v2 v2.3.0 // indirect
        github.com/santhosh-tekuri/jsonschema/v3 v3.1.0 // indirect
        github.com/swaggest/form/v5 v5.1.1 // indirect
        github.com/swaggest/jsonschema-go v0.3.78 // indirect
        github.com/swaggest/openapi-go v0.2.59 // indirect
        github.com/swaggest/refl v1.4.0 // indirect
        gopkg.in/yaml.v2 v2.4.0 // indirect
)

Test the code using cURL, with the field "enum" intentionally set to null:

curl -v 'http://localhost:8080/example' \
   -H 'accept: application/json' \
   -H 'Content-Type: application/json' \
   -d '{ "name": "Example name", "desc": "A description", "enum": null }'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions