From de83e33e51b6009849178f3b9fc4bb1c289861ea Mon Sep 17 00:00:00 2001 From: Ahmad Faiz Kamaludin Date: Tue, 14 Apr 2026 19:36:26 +0700 Subject: [PATCH] Fix null type in openapi31 parameter schemas for non-pointer types --- openapi31/reflect.go | 7 +++++++ openapi31/reflect_test.go | 4 ++-- openapi31/testdata/openapi.json | 8 ++++---- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/openapi31/reflect.go b/openapi31/reflect.go index 4be25f9..ec94ab0 100644 --- a/openapi31/reflect.go +++ b/openapi31/reflect.go @@ -432,6 +432,13 @@ func (r *Reflector) parseParametersIn( propertySchema := params.PropertySchema field := params.Field + // HTTP parameters cannot be null (only absent), so remove null type + // for non-pointer, non-ref fields. This mirrors the openapi3 reflector behavior + // (s.Schema != nil && s.Schema.Nullable != nil && field.Type.Kind() != reflect.Ptr). + if propertySchema.Ref == nil && field.Type.Kind() != reflect.Ptr { + propertySchema.RemoveType(jsonschema.Null) + } + sm, err := propertySchema.ToSchemaOrBool().ToSimpleMap() if err != nil { return err diff --git a/openapi31/reflect_test.go b/openapi31/reflect_test.go index 4747569..2474a13 100644 --- a/openapi31/reflect_test.go +++ b/openapi31/reflect_test.go @@ -507,7 +507,7 @@ func TestReflector_AddOperation_request_queryObject(t *testing.T) { { "name":"in_query","in":"query", "schema":{ - "additionalProperties":{"type":"number"},"type":["object","null"] + "additionalProperties":{"type":"number"},"type":"object" }, "style":"deepObject","explode":true } @@ -608,7 +608,7 @@ func TestReflector_AddOperation_request_jsonQuery(t *testing.T) { "name":"three","in":"query", "schema":{ "additionalProperties":{"type":"integer"}, - "type":["object","null"] + "type":"object" }, "style":"deepObject","explode":true }, diff --git a/openapi31/testdata/openapi.json b/openapi31/testdata/openapi.json index 9e2cccd..d107885 100644 --- a/openapi31/testdata/openapi.json +++ b/openapi31/testdata/openapi.json @@ -44,17 +44,17 @@ "name":"in_query3","in":"query","description":"Query parameter.","required":true, "schema":{"description":"Query parameter.","type":"integer"} }, - {"name":"array_csv","in":"query","schema":{"items":{"type":"string"},"type":["array","null"]},"explode":false}, + {"name":"array_csv","in":"query","schema":{"items":{"type":"string"},"type":"array"},"explode":false}, { - "name":"array_swg2_csv","in":"query","schema":{"items":{"type":"string"},"type":["array","null"]},"style":"form", + "name":"array_swg2_csv","in":"query","schema":{"items":{"type":"string"},"type":"array"},"style":"form", "explode":false }, { - "name":"array_swg2_ssv","in":"query","schema":{"items":{"type":"string"},"type":["array","null"]}, + "name":"array_swg2_ssv","in":"query","schema":{"items":{"type":"string"},"type":"array"}, "style":"spaceDelimited","explode":false }, { - "name":"array_swg2_pipes","in":"query","schema":{"items":{"type":"string"},"type":["array","null"]}, + "name":"array_swg2_pipes","in":"query","schema":{"items":{"type":"string"},"type":"array"}, "style":"pipeDelimited","explode":false }, {"name":"in_path","in":"path","required":true,"schema":{"type":"integer"}},