OpenAPITools / OpenAPITools/openapi-generator

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

Open
#21,661 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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
  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
  • #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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.