OpenAPITools / OpenAPITools/openapi-generator
[BUG][GO] Generated validation uses deprecated validator.v2 causing regexp validation failures
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
The Go code generator currently relies on the outdated gopkg.in/validator.v2 library, which lacks robust validation capabilities—especially for complex regex patterns. As a result, structs generated from OpenAPI specifications with regex-based validation rules often fail to validate correctly.
openapi-generator version
$ npx @openapitools/openapi-generator-cli version
7.14.0
OpenAPI declaration file content or url
Original repo
type: string
pattern: ^(?=.*\S.*)[^\x00-\x08\x0A-\x1f\x7f]{1,255}$
minLength: 1
maxLength: 255
Generation Details
Config:
generatorName: go
additionalProperties:
packageName: notification
withGoMod: false
enumClassPrefix: true
structPrefix: true
Generated result:
import (
"encoding/json"
"fmt"
"gopkg.in/validator.v2" // Generated package contains deprecated lib
)
...
type NotificationOrderItemDTO struct {
OfferId string `json:"offerId" validate:"regexp=^(?=.*\\\\S.*)[^\\\\x00-\\\\x08\\\\x0A-\\\\x1f\\\\x7f]{1,255}$"`
Count int32 `json:"count"`
}
And in UnmarshalJSON we have:
// try to unmarshal data into OrderCancelledNotificationDTO
err = newStrictDecoder(data).Decode(&dst.OrderCancelledNotificationDTO)
if err == nil {
jsonOrderCancelledNotificationDTO, _ := json.Marshal(dst.OrderCancelledNotificationDTO)
if string(jsonOrderCancelledNotificationDTO) == "{}" { // empty struct
dst.OrderCancelledNotificationDTO = nil
} else {
// This fails with: Items[0].OfferId: unknown tag
if err = validator.Validate(dst.OrderCancelledNotificationDTO); err != nil {
// Here we lost error of unknown tag usage and cannot unmarshal right JSON to dedicated struct type
dst.OrderCancelledNotificationDTO = nil
} else {
match++
}
}
} else {
dst.OrderCancelledNotificationDTO = nil
}
Steps to reproduce
git clone https://github.com/yandex-market/yandex-market-notification-api.git- Use yaml config from Generation Details section above and run:
npx @openapitools/openapi-generator-cli generate \
-c config.yaml \
-i yandex-market-notification-api/openapi/openapi.yaml \
-o client/
- Go to a
client/model_send_notification_request.gofile and you will see deprecated library - Go to a
client/model_notification_order_item_dto.gofile and you will see usage of unsupported regexp tag - Try to unmarshal some vaild JSON, e.g.
{
"campaignId": 123456,
"orderId": 123456,
"items": [
{
"count": 1,
"offerId": "TEST-SKU-1"
}
],
"cancelledAt": "2025-07-29T09:02:46.452Z",
"notificationType": "ORDER_CANCELLED"
}
And the UnmarshalJSON method will get into this branch (even though a completely valid JSON was provided):
else { // no match
return fmt.Errorf("data failed to match schemas in oneOf(SendNotificationRequest)")
}
Related issues/PRs
- #19089
- #20369
Suggest a fix
If possible, why not use a more modern validation package like https://github.com/go-playground/validator/ ?
Please correct me, I may have missed some important technical details of this choice.
Thanks.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce generation with the provided config.yaml and OpenAPI declaration, then inspect client/model_send_notification_request.go and client/model_notification_order_item_dto.go. Start by tracing the generated validator usage and the regexp tag handling during UnmarshalJSON. Done means valid JSON with the shown pattern unmarshals successfully without the deprecated validator.v2 dependency or an unsupported regexp-tag error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100