Skip to content

Better handling of backtick in pattern #19358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 16, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,43 @@ public ModelsMap postProcessModels(ModelsMap objs) {
cp.pattern.replace("\\", "\\\\").replaceAll("^/|/$", "") +
"\"");
}

// construct data tag in the template: x-go-datatag
// originl template
// `json:"{{{baseName}}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{{baseName}}}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#withValidate}} validate:"{{validate}}"{{/withValidate}}{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}`
String goDataTag = "json:\"" + cp.baseName;
if (!cp.required) {
goDataTag += ",omitempty";
}
goDataTag += "\"";

if (withXml) {
goDataTag += " xml:" + "\"" + cp.baseName;
if (cp.isXmlAttribute) {
goDataTag += ",attr";
}
goDataTag += "\"";
}

// {{#withValidate}} validate:"{{validate}}"{{/withValidate}}
if (Boolean.parseBoolean(String.valueOf(additionalProperties.getOrDefault("withValidate", "false")))) {
goDataTag += " validate:\"" + additionalProperties.getOrDefault("validate", "") + "\"";
}

// {{#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 it contains backtick, wrap with " instead
if (goDataTag.contains("`")) {
goDataTag = " \"" + goDataTag.replace("\"", "\\\"") + "\"";
} else {
goDataTag = " `" + goDataTag + "`";
}
cp.vendorExtensions.put("x-go-datatag", goDataTag);
}

if (this instanceof GoClientCodegen && model.isEnum) {
imports.add(createMapping("import", "fmt"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type {{classname}} struct {
{{#deprecated}}
// Deprecated
{{/deprecated}}
{{name}} {{^required}}{{^isNullable}}{{^isArray}}{{^isFreeFormObject}}*{{/isFreeFormObject}}{{/isArray}}{{/isNullable}}{{/required}}{{{dataType}}} `json:"{{{baseName}}}{{^required}},omitempty{{/required}}"{{#withXml}} xml:"{{{baseName}}}{{#isXmlAttribute}},attr{{/isXmlAttribute}}"{{/withXml}}{{#withValidate}} validate:"{{validate}}"{{/withValidate}}{{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}`
{{name}} {{^required}}{{^isNullable}}{{^isArray}}{{^isFreeFormObject}}*{{/isFreeFormObject}}{{/isArray}}{{/isNullable}}{{/required}}{{{dataType}}}{{{vendorExtensions.x-go-datatag}}}
{{/vars}}
{{#isAdditionalPropertiesTrue}}
AdditionalProperties map[string]interface{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1729,6 +1729,9 @@ components:
description: A string starting with 'image_' (case insensitive) and one to three digits following i.e. Image_01.
type: string
pattern: '/^image_\d{1,3}$/i'
pattern_with_backtick:
type: string
pattern: "^$|^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
EnumClass:
type: string
default: '-efg'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,10 @@ components:
to three digits following i.e. Image_01.
pattern: "/^image_\\d{1,3}$/i"
type: string
pattern_with_backtick:
pattern: "^$|^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\\
.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
type: string
required:
- byte
- date
Expand Down
26 changes: 26 additions & 0 deletions samples/openapi3/client/petstore/go/go-petstore/docs/FormatTest.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Name | Type | Description | Notes
**Password** | **string** | |
**PatternWithDigits** | Pointer to **string** | A string that is a 10 digit number. Can have leading zeros. | [optional]
**PatternWithDigitsAndDelimiter** | Pointer to **string** | A string starting with 'image_' (case insensitive) and one to three digits following i.e. Image_01. | [optional]
**PatternWithBacktick** | Pointer to **string** | | [optional]

## Methods

Expand Down Expand Up @@ -394,6 +395,31 @@ SetPatternWithDigitsAndDelimiter sets PatternWithDigitsAndDelimiter field to giv

HasPatternWithDigitsAndDelimiter returns a boolean if a field has been set.

### GetPatternWithBacktick

`func (o *FormatTest) GetPatternWithBacktick() string`

GetPatternWithBacktick returns the PatternWithBacktick field if non-nil, zero value otherwise.

### GetPatternWithBacktickOk

`func (o *FormatTest) GetPatternWithBacktickOk() (*string, bool)`

GetPatternWithBacktickOk returns a tuple with the PatternWithBacktick field if it's non-nil, zero value otherwise
and a boolean to check if the value has been set.

### SetPatternWithBacktick

`func (o *FormatTest) SetPatternWithBacktick(v string)`

SetPatternWithBacktick sets PatternWithBacktick field to given value.

### HasPatternWithBacktick

`func (o *FormatTest) HasPatternWithBacktick() bool`

HasPatternWithBacktick returns a boolean if a field has been set.


[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading