Better handling of backtick in pattern (#19358)

* use x-go-datatag in go client model template

* add logic to handle backtick
This commit is contained in:
William Cheng 2024-08-16 18:04:47 +08:00 committed by GitHub
parent cd02426648
commit a6a75e3501
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 107 additions and 1 deletions

View File

@ -786,7 +786,43 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
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"));
}

View File

@ -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{}

View File

@ -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'

View File

@ -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

View File

@ -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
@ -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)

View File

@ -39,6 +39,7 @@ type FormatTest struct {
PatternWithDigits *string `json:"pattern_with_digits,omitempty" validate:"regexp=^\\\\d{10}$"`
// A string starting with 'image_' (case insensitive) and one to three digits following i.e. Image_01.
PatternWithDigitsAndDelimiter *string `json:"pattern_with_digits_and_delimiter,omitempty" validate:"regexp=^image_\\\\d{1,3}$/i"`
PatternWithBacktick *string "json:\"pattern_with_backtick,omitempty\" validate:\"regexp=^$|^[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])?)*$\""
AdditionalProperties map[string]interface{}
}
@ -517,6 +518,38 @@ func (o *FormatTest) SetPatternWithDigitsAndDelimiter(v string) {
o.PatternWithDigitsAndDelimiter = &v
}
// GetPatternWithBacktick returns the PatternWithBacktick field value if set, zero value otherwise.
func (o *FormatTest) GetPatternWithBacktick() string {
if o == nil || IsNil(o.PatternWithBacktick) {
var ret string
return ret
}
return *o.PatternWithBacktick
}
// GetPatternWithBacktickOk returns a tuple with the PatternWithBacktick field value if set, nil otherwise
// and a boolean to check if the value has been set.
func (o *FormatTest) GetPatternWithBacktickOk() (*string, bool) {
if o == nil || IsNil(o.PatternWithBacktick) {
return nil, false
}
return o.PatternWithBacktick, true
}
// HasPatternWithBacktick returns a boolean if a field has been set.
func (o *FormatTest) HasPatternWithBacktick() bool {
if o != nil && !IsNil(o.PatternWithBacktick) {
return true
}
return false
}
// SetPatternWithBacktick gets a reference to the given string and assigns it to the PatternWithBacktick field.
func (o *FormatTest) SetPatternWithBacktick(v string) {
o.PatternWithBacktick = &v
}
func (o FormatTest) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {
@ -564,6 +597,9 @@ func (o FormatTest) ToMap() (map[string]interface{}, error) {
if !IsNil(o.PatternWithDigitsAndDelimiter) {
toSerialize["pattern_with_digits_and_delimiter"] = o.PatternWithDigitsAndDelimiter
}
if !IsNil(o.PatternWithBacktick) {
toSerialize["pattern_with_backtick"] = o.PatternWithBacktick
}
for key, value := range o.AdditionalProperties {
toSerialize[key] = value
@ -642,6 +678,7 @@ func (o *FormatTest) UnmarshalJSON(data []byte) (err error) {
delete(additionalProperties, "password")
delete(additionalProperties, "pattern_with_digits")
delete(additionalProperties, "pattern_with_digits_and_delimiter")
delete(additionalProperties, "pattern_with_backtick")
o.AdditionalProperties = additionalProperties
}