Skip to content

Commit a6a75e3

Browse files
authored
Better handling of backtick in pattern (#19358)
* use x-go-datatag in go client model template * add logic to handle backtick
1 parent cd02426 commit a6a75e3

6 files changed

Lines changed: 107 additions & 1 deletion

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,43 @@ public ModelsMap postProcessModels(ModelsMap objs) {
786786
cp.pattern.replace("\\", "\\\\").replaceAll("^/|/$", "") +
787787
"\"");
788788
}
789+
790+
// construct data tag in the template: x-go-datatag
791+
// originl template
792+
// `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}}`
793+
String goDataTag = "json:\"" + cp.baseName;
794+
if (!cp.required) {
795+
goDataTag += ",omitempty";
796+
}
797+
goDataTag += "\"";
798+
799+
if (withXml) {
800+
goDataTag += " xml:" + "\"" + cp.baseName;
801+
if (cp.isXmlAttribute) {
802+
goDataTag += ",attr";
803+
}
804+
goDataTag += "\"";
805+
}
806+
807+
// {{#withValidate}} validate:"{{validate}}"{{/withValidate}}
808+
if (Boolean.parseBoolean(String.valueOf(additionalProperties.getOrDefault("withValidate", "false")))) {
809+
goDataTag += " validate:\"" + additionalProperties.getOrDefault("validate", "") + "\"";
810+
}
811+
812+
// {{#vendorExtensions.x-go-custom-tag}} {{{.}}}{{/vendorExtensions.x-go-custom-tag}}
813+
if (StringUtils.isNotEmpty(String.valueOf(cp.vendorExtensions.getOrDefault("x-go-custom-tag", "")))) {
814+
goDataTag += " " + cp.vendorExtensions.get("x-go-custom-tag");
815+
}
816+
817+
// if it contains backtick, wrap with " instead
818+
if (goDataTag.contains("`")) {
819+
goDataTag = " \"" + goDataTag.replace("\"", "\\\"") + "\"";
820+
} else {
821+
goDataTag = " `" + goDataTag + "`";
822+
}
823+
cp.vendorExtensions.put("x-go-datatag", goDataTag);
789824
}
825+
790826
if (this instanceof GoClientCodegen && model.isEnum) {
791827
imports.add(createMapping("import", "fmt"));
792828
}

modules/openapi-generator/src/main/resources/go/model_simple.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type {{classname}} struct {
2020
{{#deprecated}}
2121
// Deprecated
2222
{{/deprecated}}
23-
{{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}}`
23+
{{name}} {{^required}}{{^isNullable}}{{^isArray}}{{^isFreeFormObject}}*{{/isFreeFormObject}}{{/isArray}}{{/isNullable}}{{/required}}{{{dataType}}}{{{vendorExtensions.x-go-datatag}}}
2424
{{/vars}}
2525
{{#isAdditionalPropertiesTrue}}
2626
AdditionalProperties map[string]interface{}

modules/openapi-generator/src/test/resources/3_0/go/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,9 @@ components:
17291729
description: A string starting with 'image_' (case insensitive) and one to three digits following i.e. Image_01.
17301730
type: string
17311731
pattern: '/^image_\d{1,3}$/i'
1732+
pattern_with_backtick:
1733+
type: string
1734+
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])?)*$"
17321735
EnumClass:
17331736
type: string
17341737
default: '-efg'

samples/openapi3/client/petstore/go/go-petstore/api/openapi.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,10 @@ components:
16831683
to three digits following i.e. Image_01.
16841684
pattern: "/^image_\\d{1,3}$/i"
16851685
type: string
1686+
pattern_with_backtick:
1687+
pattern: "^$|^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\\
1688+
.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
1689+
type: string
16861690
required:
16871691
- byte
16881692
- date

samples/openapi3/client/petstore/go/go-petstore/docs/FormatTest.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Name | Type | Description | Notes
1919
**Password** | **string** | |
2020
**PatternWithDigits** | Pointer to **string** | A string that is a 10 digit number. Can have leading zeros. | [optional]
2121
**PatternWithDigitsAndDelimiter** | Pointer to **string** | A string starting with 'image_' (case insensitive) and one to three digits following i.e. Image_01. | [optional]
22+
**PatternWithBacktick** | Pointer to **string** | | [optional]
2223

2324
## Methods
2425

@@ -394,6 +395,31 @@ SetPatternWithDigitsAndDelimiter sets PatternWithDigitsAndDelimiter field to giv
394395

395396
HasPatternWithDigitsAndDelimiter returns a boolean if a field has been set.
396397

398+
### GetPatternWithBacktick
399+
400+
`func (o *FormatTest) GetPatternWithBacktick() string`
401+
402+
GetPatternWithBacktick returns the PatternWithBacktick field if non-nil, zero value otherwise.
403+
404+
### GetPatternWithBacktickOk
405+
406+
`func (o *FormatTest) GetPatternWithBacktickOk() (*string, bool)`
407+
408+
GetPatternWithBacktickOk returns a tuple with the PatternWithBacktick field if it's non-nil, zero value otherwise
409+
and a boolean to check if the value has been set.
410+
411+
### SetPatternWithBacktick
412+
413+
`func (o *FormatTest) SetPatternWithBacktick(v string)`
414+
415+
SetPatternWithBacktick sets PatternWithBacktick field to given value.
416+
417+
### HasPatternWithBacktick
418+
419+
`func (o *FormatTest) HasPatternWithBacktick() bool`
420+
421+
HasPatternWithBacktick returns a boolean if a field has been set.
422+
397423

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

samples/openapi3/client/petstore/go/go-petstore/model_format_test_.go

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)