Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions openapi31/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,13 @@ func (r *Reflector) parseParametersIn(
propertySchema := params.PropertySchema

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Contextual Comment]
This comment refers to code near real line 426. Anchored to nearest_changed(432) line 432.


P2 | Confidence: Medium

Speculative: The fix is applied within a callback function triggered by internal.ReflectParametersIn. This function's logic and the InterceptPropParams structure are shared with the OpenAPI 3 reflector. While the provided test updates confirm the fix works for the specific test cases, there is no new unit test directly validating the logic of the null-removal condition itself (e.g., for pointer vs. non-pointer fields, or for referenced schemas). Adding a targeted unit test would ensure the behavior is explicitly documented, protected against future regressions, and clarifies the expected interaction between the jsonschema-go library's nullability and the OpenAPI reflector's parameter-specific logic.

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)
}
Comment on lines +435 to +440

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 | Confidence: High

Speculative: The fix correctly removes the "null" type from the OpenAPI 3.1 schema for non-pointer parameter fields, aligning with OpenAPI 3 behavior. However, the condition propertySchema.Ref == nil && field.Type.Kind() != reflect.Ptr is slightly broader than the OpenAPI 3 version, which also checks s.Schema.Nullable != nil. The current logic will call RemoveType(jsonschema.Null) on every non-pointer, non-ref field, even if the schema's type array doesn't actually contain jsonschema.Null. While the RemoveType method is likely idempotent, this represents a conceptual mismatch and a minor, unnecessary performance overhead for many fields. The more precise condition would check if the type array includes jsonschema.Null before attempting removal.

Code Suggestion:

if propertySchema.Ref == nil && field.Type.Kind() != reflect.Ptr && propertySchema.Type.Includes(jsonschema.Null) {
    propertySchema.RemoveType(jsonschema.Null)
}

Evidence: symbol:jsonschema.Null, method:RemoveType, path:openapi3/reflect.go


sm, err := propertySchema.ToSchemaOrBool().ToSimpleMap()
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions openapi31/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
},
Expand Down
8 changes: 4 additions & 4 deletions openapi31/testdata/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
Expand Down
Loading