Skip to content

Commit 08733aa

Browse files
authored
feat(golang): support for wrapped and xml name (OpenAPITools#21899)
* feat(golang): support for wrapped and xml name * test(golang): add two test cases for wrapped - with name - without name * chore(golang): update samples
1 parent 3f24026 commit 08733aa

File tree

10 files changed

+491
-0
lines changed

10 files changed

+491
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,9 @@ public ModelsMap postProcessModels(ModelsMap objs) {
800800

801801
if (withXml) {
802802
goDataTag += " xml:" + "\"" + cp.baseName;
803+
if (cp.isXmlWrapped) {
804+
goDataTag += ">" + ("".equals(cp.xmlName) ? cp.baseName : cp.xmlName);
805+
}
803806
if (cp.isXmlAttribute) {
804807
goDataTag += ",attr";
805808
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,26 @@ public void testAdditionalPropertiesWithoutGoMod() throws Exception {
396396
Path goSumFile = Paths.get(output + "/go.sum");
397397
TestUtils.assertFileNotExists(goSumFile);
398398
}
399+
400+
@Test
401+
public void testXmlOptionsBeingUsed() throws IOException {
402+
Map<String, Object> properties = new HashMap<>();
403+
properties.put(GoClientCodegen.STRUCT_PREFIX, true);
404+
properties.put(GoClientCodegen.WITH_XML, true);
405+
406+
File output = Files.createTempDirectory("test").toFile();
407+
output.deleteOnExit();
408+
409+
final CodegenConfigurator configurator = new CodegenConfigurator()
410+
.setGeneratorName("go")
411+
.setAdditionalProperties(properties)
412+
.setInputSpec("src/test/resources/3_0/petstore.yaml")
413+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
414+
415+
DefaultGenerator generator = new DefaultGenerator();
416+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
417+
files.forEach(File::deleteOnExit);
418+
419+
TestUtils.assertFileContains(Paths.get(output + "/model_pet.go"), "tags>tag");
420+
}
399421
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,25 @@ components:
18871887
type: array
18881888
items:
18891889
$ref: '#/components/schemas/ReadOnlyFirst'
1890+
ArrayTestWithWrapped:
1891+
type: object
1892+
properties:
1893+
array_of_string:
1894+
type: array
1895+
items:
1896+
type: string
1897+
xml:
1898+
wrapped: true
1899+
ArrayTestWithWrappedAndName:
1900+
type: object
1901+
properties:
1902+
array_of_string:
1903+
type: array
1904+
items:
1905+
type: string
1906+
xml:
1907+
name: item
1908+
wrapped: true
18901909
NumberOnly:
18911910
type: object
18921911
properties:

samples/openapi3/client/petstore/go/go-petstore/.openapi-generator/FILES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ docs/AppleReq.md
2222
docs/ArrayOfArrayOfNumberOnly.md
2323
docs/ArrayOfNumberOnly.md
2424
docs/ArrayTest.md
25+
docs/ArrayTestWithWrapped.md
26+
docs/ArrayTestWithWrappedAndName.md
2527
docs/Banana.md
2628
docs/BananaReq.md
2729
docs/Capitalization.md
@@ -106,6 +108,8 @@ model_apple_req.go
106108
model_array_of_array_of_number_only.go
107109
model_array_of_number_only.go
108110
model_array_test_.go
111+
model_array_test_with_wrapped.go
112+
model_array_test_with_wrapped_and_name.go
109113
model_banana.go
110114
model_banana_req.go
111115
model_capitalization.go

samples/openapi3/client/petstore/go/go-petstore/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ Class | Method | HTTP request | Description
139139
- [ArrayOfArrayOfNumberOnly](docs/ArrayOfArrayOfNumberOnly.md)
140140
- [ArrayOfNumberOnly](docs/ArrayOfNumberOnly.md)
141141
- [ArrayTest](docs/ArrayTest.md)
142+
- [ArrayTestWithWrapped](docs/ArrayTestWithWrapped.md)
143+
- [ArrayTestWithWrappedAndName](docs/ArrayTestWithWrappedAndName.md)
142144
- [Banana](docs/Banana.md)
143145
- [BananaReq](docs/BananaReq.md)
144146
- [Capitalization](docs/Capitalization.md)

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,25 @@ components:
18521852
type: array
18531853
type: array
18541854
type: object
1855+
ArrayTestWithWrapped:
1856+
properties:
1857+
array_of_string:
1858+
items:
1859+
type: string
1860+
type: array
1861+
xml:
1862+
wrapped: true
1863+
type: object
1864+
ArrayTestWithWrappedAndName:
1865+
properties:
1866+
array_of_string:
1867+
items:
1868+
type: string
1869+
type: array
1870+
xml:
1871+
name: item
1872+
wrapped: true
1873+
type: object
18551874
NumberOnly:
18561875
properties:
18571876
JustNumber:
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# ArrayTestWithWrapped
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**ArrayOfString** | Pointer to **[]string** | | [optional]
8+
9+
## Methods
10+
11+
### NewArrayTestWithWrapped
12+
13+
`func NewArrayTestWithWrapped() *ArrayTestWithWrapped`
14+
15+
NewArrayTestWithWrapped instantiates a new ArrayTestWithWrapped object
16+
This constructor will assign default values to properties that have it defined,
17+
and makes sure properties required by API are set, but the set of arguments
18+
will change when the set of required properties is changed
19+
20+
### NewArrayTestWithWrappedWithDefaults
21+
22+
`func NewArrayTestWithWrappedWithDefaults() *ArrayTestWithWrapped`
23+
24+
NewArrayTestWithWrappedWithDefaults instantiates a new ArrayTestWithWrapped object
25+
This constructor will only assign default values to properties that have it defined,
26+
but it doesn't guarantee that properties required by API are set
27+
28+
### GetArrayOfString
29+
30+
`func (o *ArrayTestWithWrapped) GetArrayOfString() []string`
31+
32+
GetArrayOfString returns the ArrayOfString field if non-nil, zero value otherwise.
33+
34+
### GetArrayOfStringOk
35+
36+
`func (o *ArrayTestWithWrapped) GetArrayOfStringOk() (*[]string, bool)`
37+
38+
GetArrayOfStringOk returns a tuple with the ArrayOfString field if it's non-nil, zero value otherwise
39+
and a boolean to check if the value has been set.
40+
41+
### SetArrayOfString
42+
43+
`func (o *ArrayTestWithWrapped) SetArrayOfString(v []string)`
44+
45+
SetArrayOfString sets ArrayOfString field to given value.
46+
47+
### HasArrayOfString
48+
49+
`func (o *ArrayTestWithWrapped) HasArrayOfString() bool`
50+
51+
HasArrayOfString returns a boolean if a field has been set.
52+
53+
54+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
55+
56+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# ArrayTestWithWrappedAndName
2+
3+
## Properties
4+
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**ArrayOfString** | Pointer to **[]string** | | [optional]
8+
9+
## Methods
10+
11+
### NewArrayTestWithWrappedAndName
12+
13+
`func NewArrayTestWithWrappedAndName() *ArrayTestWithWrappedAndName`
14+
15+
NewArrayTestWithWrappedAndName instantiates a new ArrayTestWithWrappedAndName object
16+
This constructor will assign default values to properties that have it defined,
17+
and makes sure properties required by API are set, but the set of arguments
18+
will change when the set of required properties is changed
19+
20+
### NewArrayTestWithWrappedAndNameWithDefaults
21+
22+
`func NewArrayTestWithWrappedAndNameWithDefaults() *ArrayTestWithWrappedAndName`
23+
24+
NewArrayTestWithWrappedAndNameWithDefaults instantiates a new ArrayTestWithWrappedAndName object
25+
This constructor will only assign default values to properties that have it defined,
26+
but it doesn't guarantee that properties required by API are set
27+
28+
### GetArrayOfString
29+
30+
`func (o *ArrayTestWithWrappedAndName) GetArrayOfString() []string`
31+
32+
GetArrayOfString returns the ArrayOfString field if non-nil, zero value otherwise.
33+
34+
### GetArrayOfStringOk
35+
36+
`func (o *ArrayTestWithWrappedAndName) GetArrayOfStringOk() (*[]string, bool)`
37+
38+
GetArrayOfStringOk returns a tuple with the ArrayOfString field if it's non-nil, zero value otherwise
39+
and a boolean to check if the value has been set.
40+
41+
### SetArrayOfString
42+
43+
`func (o *ArrayTestWithWrappedAndName) SetArrayOfString(v []string)`
44+
45+
SetArrayOfString sets ArrayOfString field to given value.
46+
47+
### HasArrayOfString
48+
49+
`func (o *ArrayTestWithWrappedAndName) HasArrayOfString() bool`
50+
51+
HasArrayOfString returns a boolean if a field has been set.
52+
53+
54+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
55+
56+

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

Lines changed: 155 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)