Skip to content

[BUG] Golang pattern validation with regex fails on commas #20079

@DVasselli

Description

@DVasselli

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • 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

When having a struct with a oneOf keyword which has an item with a regex validation, if the regex has a comma, the validation will always fail.

For example, if I want a number to be present 2 or more times [0-9]{2,}, this would fail since the validator library wants us to escape the comma.

Furthermore, the error message says data failed to match schemas in oneOf which is misleading since the error is really coming from the regex validation.

openapi-generator version

7.9.0

OpenAPI declaration file content or url
openapi: '3.0.3'
info:
  title: API Test
  version: '1.0'
paths:
  /foo:
    get:
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: object
                properties:
                  code:
                    oneOf:
                      - $ref: "#/components/schemas/importCode"

components:
  schemas:
    importCode:
      type: object
      properties:
        code:
          type: string
          pattern: "^[0-9]{2,}$"
Generation Details

From the folder where you put the yml file (I am working in ~/playground):

openapi-generator generate -i api_test.yml \
    -g go \
    -o ~/playground/gen \
    --package-name=foo \
    --global-property=verbose=false,apiDocs=false,apiTests=false,modelDocs=false,modelTests=false \
    --additional-properties=isGoSubmodule=true,withGoMod=false
Steps to reproduce
  1. Generate the file yml definition and the command above.
  2. Create a foo_test.go file like the following:
package playground

import (
	"encoding/json"
	foo "foo/gen"
	"testing"
)

func TestFoo(t *testing.T) {
	f1 := foo.FooGet200Response{
		Code: &foo.FooGet200ResponseCode{
			ImportCode: &foo.ImportCode{
				Code: Get("12"),
			},
		},
	}

	m, err := json.Marshal(f1)
	if err != nil {
		panic(err)
	}

	f2 := &foo.FooGet200Response{}
	err = json.Unmarshal(m, f2)
	if err != nil {
		panic(err)
	}
}

func Get[T any](v T) *T {
	return &v
}

This will fail at unmarshalling no matter how many digits you put on that string.
3. To prove the point, edit the yml file removing the comma, regenerate, and provide exactly 2 digits in the test. This will succeed.
4. Bonus point, if on the file with an exact number of repetition (without the comma) you put more than expected, the error will say data failed to match schemas in oneOf which is too generic for regex validation failure.

Related issues/PRs

None

Suggest a fix

From where I stand, there are 2 possible issues:

  1. Remove this manipulation and accept the regex as it is provided by the user
    cp.pattern.replace("\\", "\\\\").replaceAll("^/|/$", "") +
    "\"");
    }
  2. The current library is expecting an invalid regex syntax, and it could be replaced with any other that does not have this issue

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