diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java index 841a0fa6814..9f65605bc69 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java @@ -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")); } diff --git a/modules/openapi-generator/src/main/resources/go/model_simple.mustache b/modules/openapi-generator/src/main/resources/go/model_simple.mustache index 30926bf14d2..a2d1b7443a0 100644 --- a/modules/openapi-generator/src/main/resources/go/model_simple.mustache +++ b/modules/openapi-generator/src/main/resources/go/model_simple.mustache @@ -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{} diff --git a/modules/openapi-generator/src/test/resources/3_0/go/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml b/modules/openapi-generator/src/test/resources/3_0/go/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml index 766fde557fe..f02e8d9e6c2 100644 --- a/modules/openapi-generator/src/test/resources/3_0/go/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/go/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml @@ -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' diff --git a/samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml b/samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml index 525485baf7b..3a8b28a21ae 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml +++ b/samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml @@ -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 diff --git a/samples/openapi3/client/petstore/go/go-petstore/docs/FormatTest.md b/samples/openapi3/client/petstore/go/go-petstore/docs/FormatTest.md index 2e2ed889929..273efb4c11c 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/docs/FormatTest.md +++ b/samples/openapi3/client/petstore/go/go-petstore/docs/FormatTest.md @@ -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) diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_format_test_.go b/samples/openapi3/client/petstore/go/go-petstore/model_format_test_.go index 277327aa1be..e7529f6d453 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_format_test_.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_format_test_.go @@ -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 }