Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/samples-go-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
- samples/client/others/go/allof_multiple_ref_and_discriminator/**
- samples/client/others/go/oneof-anyof-required/**
- samples/client/others/go/oneof-discriminator-lookup/**
- samples/client/others/go/issue_20079_go_regex_wrongly_translated/**
pull_request:
paths:
- 'samples/openapi3/client/petstore/go/go-petstore-aws-signature/**'
Expand All @@ -17,6 +18,7 @@ on:
- samples/client/others/go/allof_multiple_ref_and_discriminator/**
- samples/client/others/go/oneof-anyof-required/**
- samples/client/others/go/oneof-discriminator-lookup/**
- samples/client/others/go/issue_20079_go_regex_wrongly_translated/**
jobs:
build:
name: Build Go
Expand All @@ -31,6 +33,7 @@ jobs:
- samples/client/others/go/allof_multiple_ref_and_discriminator/
- samples/client/others/go/oneof-anyof-required/
- samples/client/others/go/oneof-discriminator-lookup/
- samples/client/others/go/issue_20079_go_regex_wrongly_translated/
steps:
- uses: actions/checkout@v7
- uses: actions/setup-go@v7
Expand Down
11 changes: 11 additions & 0 deletions bin/configs/go-regex-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
generatorName: go
outputDir: samples/client/others/go/issue_20079_go_regex_wrongly_translated/
inputSpec: modules/openapi-generator/src/test/resources/3_0/issue_20079_go_regex_wrongly_translated.yaml
templateDir: modules/openapi-generator/src/main/resources/go
#additionalProperties:
# enumClassPrefix: "true"
# packageName: petstore
# disallowAdditionalPropertiesIfNotPresent: false
# generateInterfaces: true
# useDefaultValuesForRequiredVars: "true"
# enumUnknownDefaultCase: true
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,11 @@ public String toEnumVarName(String value, String datatype) {
public boolean specVersionGreaterThanOrEqualTo310(OpenAPI openAPI) {
String originalSpecVersion;
String xOriginalSwaggerVersion = "x-original-swagger-version";

if (openAPI == null) {
return false;
}

if (openAPI.getExtensions() != null && !openAPI.getExtensions().isEmpty() && openAPI.getExtensions().containsValue(xOriginalSwaggerVersion)) {
originalSpecVersion = (String) openAPI.getExtensions().get(xOriginalSwaggerVersion);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.io.File;
import java.util.*;
import java.util.regex.Matcher;

import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER;
import static org.openapitools.codegen.utils.StringUtils.camelize;
Expand All @@ -38,6 +39,7 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege

private final Logger LOGGER = LoggerFactory.getLogger(AbstractGoCodegen.class);
private static final String NUMERIC_ENUM_PREFIX = "_";
private static final String X_GO_CUSTOM_TAG = "x-go-custom-tag";

@Setter
protected boolean withGoCodegenComment = false;
Expand Down Expand Up @@ -804,9 +806,20 @@ public ModelsMap postProcessModels(ModelsMap objs) {
}

if (cp.pattern != null) {
cp.vendorExtensions.put("x-go-custom-tag", "validate:\"regexp=" +
cp.pattern.replace("\\", "\\\\").replaceAll("^/|/$", "") +
"\"");
String regexp = String.format(Locale.getDefault(), "regexp=%s", cp.pattern);

// Replace backtick by \\x60, if found
if (regexp.contains("`")) {
regexp = regexp.replace("`", "\\x60");
}

// Escape comma
if (regexp.contains(",")) {
regexp = regexp.replace(",", "\\\\,");
}

String validate = String.format(Locale.getDefault(), "validate:\"%s\"", regexp);
cp.vendorExtensions.put(X_GO_CUSTOM_TAG, validate);
}

// construct data tag in the template: x-go-datatag
Expand Down Expand Up @@ -835,8 +848,8 @@ public ModelsMap postProcessModels(ModelsMap objs) {
}

// {{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}
if (StringUtils.isNotEmpty(String.valueOf(cp.vendorExtensions.getOrDefault("x-go-custom-tag", "")))) {
goDataTag += " " + cp.vendorExtensions.get("x-go-custom-tag");
if (StringUtils.isNotEmpty(String.valueOf(cp.vendorExtensions.getOrDefault(X_GO_CUSTOM_TAG, "")))) {
goDataTag += " " + cp.vendorExtensions.get(X_GO_CUSTOM_TAG);
}

// if it contains backtick, wrap with " instead
Expand Down Expand Up @@ -891,6 +904,11 @@ public ModelsMap postProcessModels(ModelsMap objs) {
return postProcessModelsEnum(objs);
}

@Override
public String addRegularExpressionDelimiter(String pattern) {
return pattern;
}

@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
generateYAMLSpecFile(objs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,16 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
} else if match == 1 {
return nil // exactly one match
} else { // no match
return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
{{#oneOf}}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: {{#oneOf}} iteration in the "no match" else block generates duplicate Go code for types with multiple oneOf schemas. The template content only references {{classname}} (same every iteration), not {{{.}}} (current item), so each iteration produces an identical if err != nil { return ... } else { return ... } block. For types with 2+ oneOf items, all blocks after the first are unreachable dead code. This is a pre-existing pattern (original {{#oneOf}}return fmt.Errorf(...){{/oneOf}} had the same issue), but since this PR restructures the same section, consider removing the {{#oneOf}} wrapper entirely and keeping only the single non-iterated block with the {{^oneOf}} fallback.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/go/model_oneof.mustache, line 83:

<comment>`{{#oneOf}}` iteration in the "no match" else block generates duplicate Go code for types with multiple oneOf schemas. The template content only references `{{classname}}` (same every iteration), not `{{{.}}}` (current item), so each iteration produces an identical `if err != nil { return ... } else { return ... }` block. For types with 2+ oneOf items, all blocks after the first are unreachable dead code. This is a pre-existing pattern (original `{{#oneOf}}return fmt.Errorf(...){{/oneOf}}` had the same issue), but since this PR restructures the same section, consider removing the `{{#oneOf}}` wrapper entirely and keeping only the single non-iterated block with the `{{^oneOf}}` fallback.</comment>

<file context>
@@ -80,7 +80,16 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
 		return nil // exactly one match
 	} else { // no match
-		return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
+        {{#oneOf}}
+        if err != nil {
+		   return fmt.Errorf("data failed to match schemas in oneOf({{classname}}): %v", err)
</file context>

if err != nil {
return fmt.Errorf("data failed to match schemas in oneOf({{classname}}): %v", err)
} else {
return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
}
{{/oneOf}}
{{^oneOf}}
return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
{{/oneOf}}
}
{{/discriminator}}
{{/useOneOfDiscriminatorLookup}}
Expand Down Expand Up @@ -115,7 +124,16 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
} else if match == 1 {
return nil // exactly one match
} else { // no match
return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
{{#oneOf}}
if err != nil {
return fmt.Errorf("data failed to match schemas in oneOf({{classname}}): %v", err)
} else {
return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
}
{{/oneOf}}
{{^oneOf}}
return fmt.Errorf("data failed to match schemas in oneOf({{classname}})")
{{/oneOf}}
}
{{/useOneOfDiscriminatorLookup}}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
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,}$"

creditCard:
description: "Visa credit card\n
matches: 4123 6453 2222 1746\n
non-matches: 3124 5675 4400 4567, 4123-6453-2222-1746"
type: string

pattern: "^4[0-9]{3}\\s[0-9]{4}\\s[0-9]{4}\\s[0-9]{4}$"
# Original was: 4[0-9]{3}\s[0-9]{4}\s[0-9]{4}\s[0-9]{4}

date:
description: "Some dates\n
matches: 31/04/1999, 15/12/4567\n
non-matches: 31/4/1999, 31/4/99, 1999/04/19, 42/67/25456"
type: string
pattern: "^([0-2][0-9]|30|31)/(0[1-9]|1[0-2])/[0-9]{4}$"
# Original was: ([0-2][0-9]|30|31)/(0[1-9]|1[0-2])/[0-9]{4} : unchanged

windowsAbsolutePath:
description: "Windows absolute path\n
matches: \\\\server\\share\\file\n
non-matches: \\directory\\directory2, /directory2"
type: string

# This test case doesn't work due to a problem (?) in validator.v2 (?)
# it issues an unexpected unknown tag or Bad Parameter.

# pattern: "^([A-Za-z]:|\\)\\[[:alnum:][:whitespace:]!\"#$%&'()+,-.;=@[]^_`{}~.]*$"
# Original was: ([A-Za-z]:|\\)\\[[:alnum:][:whitespace:]!"#$%&'()+,-.\\;=@\[\]^_`{}~.]*

email1:
description: "Email Address 1\n
matches: abc.123@def456.com, _123@abc.ca\n
non-matches: abc@dummy, ab*cd@efg.hijkl"
type: string

pattern: "^[[:word:]\\-.]+@[[:word:]\\-.]+\\.[[:alpha:]]{2,3}$"
# Original was: [[:word:]\-.]+@[[:word:]\-.]+\.[[:alpha:]]{2,3}

email2:
description: "Email Address 2\n
matches: *@qrstuv@wxyz.12345.com, __1234^%@@abc.def.ghijkl\n
non-matches: abc.123.*&ca, ^%abcdefg123"
type: string

pattern: "^.+@.+\\..+$"
# Original was: .+@.+\..+

htmlHexadecimalColorCode1:
description: "HTML Hexadecimal Color Code 1\n
matches: AB1234, CCCCCC, 12AF3B\n
non-matches: 123G45, 12-44-CC"
type: string
pattern: "^[A-F0-9]{6}$"
# Original was: [A-F0-9]{6} : unchanged

htmlHexadecimalColorCode2:
description: "HTML Hexadecimal Color Code 2\n
matches: AB 11 00, CC 12 D3\n
non-matches: SS AB CD, AA BB CC DD, 1223AB"
type: string

pattern: "^[A-F0-9]{2}\\s[A-F0-9]{2}\\s[A-F0-9]{2}$"
# Original was: [A-F0-9]{2}\s[A-F0-9]{2}\s[A-F0-9]{2}

ipAddress:
description: "IP Address\n
matches: 10.25.101.216\n
non-matches: 0.0.0, 256.89.457.02"
type: string

pattern: "^((2(5[0-5]|[0-4][0-9])|1([0-9][0-9])|([1-9][0-9])|[0-9])\\.){3}(2(5[0-5]|[0-4][0-9])|1([0-9][0-9])|([1-9][0-9])|[0-9])$"
# Original was: ((2(5[0-5]|[0-4][0-9])|1([0-9][0-9])|([1-9][0-9])|[0-9])\.){3}(2(5[0-5]|[0-4][0-9])|1([0-9][0-9])|([1-9][0-9])|[0-9])

javaComments:
description: "Java Comments\n
matches: Matches Java comments that are between /* and */, or one line comments prefaced by //\n
non-matches: a=1"
type: string

# This test case doesn't work due to a problem (?) in validator.v2 (?)
# org.yaml.snakeyaml.scanner declares \* being an invalid escape code at yaml checking step

# pattern: "^/\*.*\*/|//[^\\n]*$"
# Original was: /\*.*\*/|//[^\n]*

money:
description: "\n
matches: $1.00, -$97.65
non-matches: $1, 1.00$, $-75.17"
type: string

pattern: "^(\\+|-)?\\$[0-9]*\\.[0-9]{2}$"
# Original was: (\+|-)?\$[0-9]*\.[0-9]{2}

positiveNegativeDecimalValue:
description: "Positive, negative numbers, and decimal values\n
matches: +41, -412, 2, 7968412, 41, +41.1, -3.141592653
non-matches: ++41, 41.1.19, -+97.14"
type: string

pattern: "^(\\+|-)?[0-9]+(\\.[0-9]+)?$"
# Original was: (\+|-)?[0-9]+(\.[0-9]+)?

password1:
description: "Passwords 1\n
matches: abcd, 1234, A1b2C3d4, 1a2B3\n
non-matches: abc, *ab12, abcdefghijkl"
type: string
pattern: "^[[:alnum:]]{4,10}$"
# Original was: [[:alnum:]]{4,10} : unchanged

password2:
description: "Passwords 2\n
matches: AB_cd, A1_b2c3, a123_\n
non-matches: *&^g, abc, 1bcd"
type: string

pattern: "^[a-zA-Z]\\w{3,7}$"
# Original was: [a-zA-Z]\w{3,7} : unchanged

phoneNumber:
description: "Phone Numbers\n
matches: 519-883-6898, 519 888 6898\n
non-matches: 888 6898, 5198886898, 519 883-6898"
type: string

pattern: "^([2-9][0-9]{2}-[2-9][0-9]{2}-[0-9]{4})|([2-9][0-9]{2}\\s[2-9][0-9]{2}\\s[0-9]{4})$"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The phoneNumber regex anchors don't apply to both alternatives. ^ only anchors the hyphen-separated branch and $ only anchors the space-separated branch, so abc 519 888 6898 or 519-883-6898 xyz would incorrectly match. Consider wrapping both alternatives in a group anchored at both ends: ^(([2-9][0-9]{2}-[2-9][0-9]{2}-[0-9]{4})|([2-9][0-9]{2}\s[2-9][0-9]{2}\s[0-9]{4}))$.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/test/resources/3_0/issue_20079_go_regex_wrongly_translated.yaml, line 155:

<comment>The phoneNumber regex anchors don't apply to both alternatives. `^` only anchors the hyphen-separated branch and `$` only anchors the space-separated branch, so `abc 519 888 6898` or `519-883-6898 xyz` would incorrectly match. Consider wrapping both alternatives in a group anchored at both ends: `^(([2-9][0-9]{2}-[2-9][0-9]{2}-[0-9]{4})|([2-9][0-9]{2}\s[2-9][0-9]{2}\s[0-9]{4}))$`.</comment>

<file context>
@@ -0,0 +1,191 @@
+            non-matches: 888 6898, 5198886898, 519 883-6898"
+          type: string
+
+          pattern: "^([2-9][0-9]{2}-[2-9][0-9]{2}-[0-9]{4})|([2-9][0-9]{2}\\s[2-9][0-9]{2}\\s[0-9]{4})$"
+          # Original was: ([2-9][0-9]{2}-[2-9][0-9]{2}-[0-9]{4})|([2-9][0-9]{2}\s[2-9][0-9]{2}\s[0-9]{4})
+
</file context>
Suggested change
pattern: "^([2-9][0-9]{2}-[2-9][0-9]{2}-[0-9]{4})|([2-9][0-9]{2}\\s[2-9][0-9]{2}\\s[0-9]{4})$"
pattern: "^(([2-9][0-9]{2}-[2-9][0-9]{2}-[0-9]{4})|([2-9][0-9]{2}\\s[2-9][0-9]{2}\\s[0-9]{4}))$"

# Original was: ([2-9][0-9]{2}-[2-9][0-9]{2}-[0-9]{4})|([2-9][0-9]{2}\s[2-9][0-9]{2}\s[0-9]{4})

sentence1:
description: "Sentences 1\n
matches: Hello, how are you?\n
non-matches: i am fine"
type: string

pattern: "^[A-Z0-9].*(\\.|\\?|!)$"
# Original was: [A-Z0-9].*(\.|\?|!)

sentence2:
description: "Sentences 2\n
matches: Hello, how are you?n
non-matches: i am fine"
type: string
pattern: "^[[:upper:]0-9].*[.?!]$"
# Original was: [[:upper:]0-9].*[.?!] : unchanged

socialSecurityNumber:
description: "Social Security Number\n
matches: 123-45-6789\n
non-matches: 123 45 6789, 123456789, 1234-56-7891"
type: string
pattern: "^[0-9]{3}-[0-9]{2}-[0-9]{4}$"
# Original was: [0-9]{3}-[0-9]{2}-[0-9]{4} : unchanged

url:
description: "URL\n
matches: http://www.sample.com, www.sample.com\n
non-matches: http://sample.com, http://www.sample.comm"
type: string

# \. ==> \\.
pattern: "^(http://)?www\\.[a-zA-Z0-9]+\\.[a-zA-Z]{2,3}$"
# Original was: (http://)?www\.[a-zA-Z0-9]+\.[a-zA-Z]{2,3}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md
Loading
Loading