From 847661acc2380309251ed1faf57711b7598c6270 Mon Sep 17 00:00:00 2001 From: danidomi Date: Sat, 18 Oct 2025 14:40:24 +0100 Subject: [PATCH 1/2] add allowEmptyValue --- dsl/validation.go | 11 +++++++++++ expr/attribute.go | 3 +++ http/codegen/openapi/v3/parameters.go | 7 ++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/dsl/validation.go b/dsl/validation.go index 4244a0b48c..cd9b80277e 100644 --- a/dsl/validation.go +++ b/dsl/validation.go @@ -381,6 +381,17 @@ func MinLength(val int) { } } +// AllowEmptyValue sets the AllowEmptyValue flag for a query or header parameter. +// If not set, default behavior in paramFor is in != "path". +func AllowEmptyValue(val bool) { + if a, ok := eval.Current().(*expr.AttributeExpr); ok { + if a.Validation == nil { + a.Validation = &expr.ValidationExpr{} + } + a.Validation.AllowEmptyValue = val + } +} + // MaxLength adds a "maxItems" validation to the attribute. // See http://json-schema.org/latest/json-schema-validation.html#anchor42. // diff --git a/expr/attribute.go b/expr/attribute.go index 8f1ef9a181..73941500a3 100644 --- a/expr/attribute.go +++ b/expr/attribute.go @@ -98,6 +98,9 @@ type ( // described at // http://json-schema.org/latest/json-schema-validation.html#anchor61. Required []string + // AllowEmptyValue indicates whether the attribute allows empty + // values. This is primarily used for query string parameters. + AllowEmptyValue bool } // ValidationFormat is the type used to enumerate the possible string diff --git a/http/codegen/openapi/v3/parameters.go b/http/codegen/openapi/v3/parameters.go index b916a08d4d..4fedfbeea3 100644 --- a/http/codegen/openapi/v3/parameters.go +++ b/http/codegen/openapi/v3/parameters.go @@ -55,11 +55,16 @@ func paramsFromHeadersAndCookies(endpoint *expr.HTTPEndpointExpr, rand *expr.Exa // paramFor converts the given attribute into a OpenAPI spec parameter. func paramFor(att *expr.AttributeExpr, name, in string, required bool, rand *expr.ExampleGenerator) *Parameter { + allowEmpty := in != "path" + if att.Validation != nil { + allowEmpty = att.Validation.AllowEmptyValue + } + param := &Parameter{ Name: name, In: in, Description: att.Description, - AllowEmptyValue: in != "path", + AllowEmptyValue: allowEmpty, Required: required, Schema: newSchemafier(rand).schemafy(att), Extensions: openapi.ExtensionsFromExpr(att.Meta), From 4fc81724b76e584aeabb54c314f1345a5ded3ea7 Mon Sep 17 00:00:00 2001 From: danidomi Date: Sun, 19 Oct 2025 18:54:37 +0100 Subject: [PATCH 2/2] add AllowEmptyValue bool --- dsl/validation.go | 2 +- expr/attribute.go | 2 +- http/codegen/openapi/v3/parameters.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dsl/validation.go b/dsl/validation.go index cd9b80277e..7aaa652ec1 100644 --- a/dsl/validation.go +++ b/dsl/validation.go @@ -388,7 +388,7 @@ func AllowEmptyValue(val bool) { if a.Validation == nil { a.Validation = &expr.ValidationExpr{} } - a.Validation.AllowEmptyValue = val + a.Validation.AllowEmptyValue = &val } } diff --git a/expr/attribute.go b/expr/attribute.go index 73941500a3..baf58c467b 100644 --- a/expr/attribute.go +++ b/expr/attribute.go @@ -100,7 +100,7 @@ type ( Required []string // AllowEmptyValue indicates whether the attribute allows empty // values. This is primarily used for query string parameters. - AllowEmptyValue bool + AllowEmptyValue *bool } // ValidationFormat is the type used to enumerate the possible string diff --git a/http/codegen/openapi/v3/parameters.go b/http/codegen/openapi/v3/parameters.go index 4fedfbeea3..3fde3b6be0 100644 --- a/http/codegen/openapi/v3/parameters.go +++ b/http/codegen/openapi/v3/parameters.go @@ -56,8 +56,8 @@ func paramsFromHeadersAndCookies(endpoint *expr.HTTPEndpointExpr, rand *expr.Exa // paramFor converts the given attribute into a OpenAPI spec parameter. func paramFor(att *expr.AttributeExpr, name, in string, required bool, rand *expr.ExampleGenerator) *Parameter { allowEmpty := in != "path" - if att.Validation != nil { - allowEmpty = att.Validation.AllowEmptyValue + if att.Validation != nil && att.Validation.AllowEmptyValue != nil { + allowEmpty = *att.Validation.AllowEmptyValue } param := &Parameter{