Skip to content

[BUG][GO] Generated validation uses deprecated validator.v2 causing regexp validation failures #21661

@IceTweak

Description

@IceTweak

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
  1. git clone https://github.com/yandex-market/yandex-market-notification-api.git
  2. 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/
  1. Go to a client/model_send_notification_request.go file and you will see deprecated library
  2. Go to a client/model_notification_order_item_dto.go file and you will see usage of unsupported regexp tag
  3. 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
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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions