rest icon indicating copy to clipboard operation
rest copied to clipboard

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

Open dmlambea opened this issue 3 months ago • 1 comments

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 }'

dmlambea avatar Sep 05 '25 11:09 dmlambea