feat(golang): support for wrapped and xml name (#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
This commit is contained in:
Sebastian Rühl
2025-09-04 19:14:14 +02:00
committed by GitHub
parent 3f24026b87
commit 08733aaf25
10 changed files with 491 additions and 0 deletions

View File

@@ -800,6 +800,9 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
if (withXml) {
goDataTag += " xml:" + "\"" + cp.baseName;
if (cp.isXmlWrapped) {
goDataTag += ">" + ("".equals(cp.xmlName) ? cp.baseName : cp.xmlName);
}
if (cp.isXmlAttribute) {
goDataTag += ",attr";
}

View File

@@ -396,4 +396,26 @@ public class GoClientCodegenTest {
Path goSumFile = Paths.get(output + "/go.sum");
TestUtils.assertFileNotExists(goSumFile);
}
@Test
public void testXmlOptionsBeingUsed() throws IOException {
Map<String, Object> properties = new HashMap<>();
properties.put(GoClientCodegen.STRUCT_PREFIX, true);
properties.put(GoClientCodegen.WITH_XML, true);
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("go")
.setAdditionalProperties(properties)
.setInputSpec("src/test/resources/3_0/petstore.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
files.forEach(File::deleteOnExit);
TestUtils.assertFileContains(Paths.get(output + "/model_pet.go"), "tags>tag");
}
}

View File

@@ -1887,6 +1887,25 @@ components:
type: array
items:
$ref: '#/components/schemas/ReadOnlyFirst'
ArrayTestWithWrapped:
type: object
properties:
array_of_string:
type: array
items:
type: string
xml:
wrapped: true
ArrayTestWithWrappedAndName:
type: object
properties:
array_of_string:
type: array
items:
type: string
xml:
name: item
wrapped: true
NumberOnly:
type: object
properties:

View File

@@ -22,6 +22,8 @@ docs/AppleReq.md
docs/ArrayOfArrayOfNumberOnly.md
docs/ArrayOfNumberOnly.md
docs/ArrayTest.md
docs/ArrayTestWithWrapped.md
docs/ArrayTestWithWrappedAndName.md
docs/Banana.md
docs/BananaReq.md
docs/Capitalization.md
@@ -106,6 +108,8 @@ model_apple_req.go
model_array_of_array_of_number_only.go
model_array_of_number_only.go
model_array_test_.go
model_array_test_with_wrapped.go
model_array_test_with_wrapped_and_name.go
model_banana.go
model_banana_req.go
model_capitalization.go

View File

@@ -139,6 +139,8 @@ Class | Method | HTTP request | Description
- [ArrayOfArrayOfNumberOnly](docs/ArrayOfArrayOfNumberOnly.md)
- [ArrayOfNumberOnly](docs/ArrayOfNumberOnly.md)
- [ArrayTest](docs/ArrayTest.md)
- [ArrayTestWithWrapped](docs/ArrayTestWithWrapped.md)
- [ArrayTestWithWrappedAndName](docs/ArrayTestWithWrappedAndName.md)
- [Banana](docs/Banana.md)
- [BananaReq](docs/BananaReq.md)
- [Capitalization](docs/Capitalization.md)

View File

@@ -1852,6 +1852,25 @@ components:
type: array
type: array
type: object
ArrayTestWithWrapped:
properties:
array_of_string:
items:
type: string
type: array
xml:
wrapped: true
type: object
ArrayTestWithWrappedAndName:
properties:
array_of_string:
items:
type: string
type: array
xml:
name: item
wrapped: true
type: object
NumberOnly:
properties:
JustNumber:

View File

@@ -0,0 +1,56 @@
# ArrayTestWithWrapped
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**ArrayOfString** | Pointer to **[]string** | | [optional]
## Methods
### NewArrayTestWithWrapped
`func NewArrayTestWithWrapped() *ArrayTestWithWrapped`
NewArrayTestWithWrapped instantiates a new ArrayTestWithWrapped object
This constructor will assign default values to properties that have it defined,
and makes sure properties required by API are set, but the set of arguments
will change when the set of required properties is changed
### NewArrayTestWithWrappedWithDefaults
`func NewArrayTestWithWrappedWithDefaults() *ArrayTestWithWrapped`
NewArrayTestWithWrappedWithDefaults instantiates a new ArrayTestWithWrapped object
This constructor will only assign default values to properties that have it defined,
but it doesn't guarantee that properties required by API are set
### GetArrayOfString
`func (o *ArrayTestWithWrapped) GetArrayOfString() []string`
GetArrayOfString returns the ArrayOfString field if non-nil, zero value otherwise.
### GetArrayOfStringOk
`func (o *ArrayTestWithWrapped) GetArrayOfStringOk() (*[]string, bool)`
GetArrayOfStringOk returns a tuple with the ArrayOfString field if it's non-nil, zero value otherwise
and a boolean to check if the value has been set.
### SetArrayOfString
`func (o *ArrayTestWithWrapped) SetArrayOfString(v []string)`
SetArrayOfString sets ArrayOfString field to given value.
### HasArrayOfString
`func (o *ArrayTestWithWrapped) HasArrayOfString() bool`
HasArrayOfString 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

@@ -0,0 +1,56 @@
# ArrayTestWithWrappedAndName
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**ArrayOfString** | Pointer to **[]string** | | [optional]
## Methods
### NewArrayTestWithWrappedAndName
`func NewArrayTestWithWrappedAndName() *ArrayTestWithWrappedAndName`
NewArrayTestWithWrappedAndName instantiates a new ArrayTestWithWrappedAndName object
This constructor will assign default values to properties that have it defined,
and makes sure properties required by API are set, but the set of arguments
will change when the set of required properties is changed
### NewArrayTestWithWrappedAndNameWithDefaults
`func NewArrayTestWithWrappedAndNameWithDefaults() *ArrayTestWithWrappedAndName`
NewArrayTestWithWrappedAndNameWithDefaults instantiates a new ArrayTestWithWrappedAndName object
This constructor will only assign default values to properties that have it defined,
but it doesn't guarantee that properties required by API are set
### GetArrayOfString
`func (o *ArrayTestWithWrappedAndName) GetArrayOfString() []string`
GetArrayOfString returns the ArrayOfString field if non-nil, zero value otherwise.
### GetArrayOfStringOk
`func (o *ArrayTestWithWrappedAndName) GetArrayOfStringOk() (*[]string, bool)`
GetArrayOfStringOk returns a tuple with the ArrayOfString field if it's non-nil, zero value otherwise
and a boolean to check if the value has been set.
### SetArrayOfString
`func (o *ArrayTestWithWrappedAndName) SetArrayOfString(v []string)`
SetArrayOfString sets ArrayOfString field to given value.
### HasArrayOfString
`func (o *ArrayTestWithWrappedAndName) HasArrayOfString() bool`
HasArrayOfString 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

@@ -0,0 +1,155 @@
/*
OpenAPI Petstore
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
API version: 1.0.0
*/
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
package petstore
import (
"encoding/json"
)
// checks if the ArrayTestWithWrapped type satisfies the MappedNullable interface at compile time
var _ MappedNullable = &ArrayTestWithWrapped{}
// ArrayTestWithWrapped struct for ArrayTestWithWrapped
type ArrayTestWithWrapped struct {
ArrayOfString []string `json:"array_of_string,omitempty"`
AdditionalProperties map[string]interface{}
}
type _ArrayTestWithWrapped ArrayTestWithWrapped
// NewArrayTestWithWrapped instantiates a new ArrayTestWithWrapped object
// This constructor will assign default values to properties that have it defined,
// and makes sure properties required by API are set, but the set of arguments
// will change when the set of required properties is changed
func NewArrayTestWithWrapped() *ArrayTestWithWrapped {
this := ArrayTestWithWrapped{}
return &this
}
// NewArrayTestWithWrappedWithDefaults instantiates a new ArrayTestWithWrapped object
// This constructor will only assign default values to properties that have it defined,
// but it doesn't guarantee that properties required by API are set
func NewArrayTestWithWrappedWithDefaults() *ArrayTestWithWrapped {
this := ArrayTestWithWrapped{}
return &this
}
// GetArrayOfString returns the ArrayOfString field value if set, zero value otherwise.
func (o *ArrayTestWithWrapped) GetArrayOfString() []string {
if o == nil || IsNil(o.ArrayOfString) {
var ret []string
return ret
}
return o.ArrayOfString
}
// GetArrayOfStringOk returns a tuple with the ArrayOfString field value if set, nil otherwise
// and a boolean to check if the value has been set.
func (o *ArrayTestWithWrapped) GetArrayOfStringOk() ([]string, bool) {
if o == nil || IsNil(o.ArrayOfString) {
return nil, false
}
return o.ArrayOfString, true
}
// HasArrayOfString returns a boolean if a field has been set.
func (o *ArrayTestWithWrapped) HasArrayOfString() bool {
if o != nil && !IsNil(o.ArrayOfString) {
return true
}
return false
}
// SetArrayOfString gets a reference to the given []string and assigns it to the ArrayOfString field.
func (o *ArrayTestWithWrapped) SetArrayOfString(v []string) {
o.ArrayOfString = v
}
func (o ArrayTestWithWrapped) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {
return []byte{}, err
}
return json.Marshal(toSerialize)
}
func (o ArrayTestWithWrapped) ToMap() (map[string]interface{}, error) {
toSerialize := map[string]interface{}{}
if !IsNil(o.ArrayOfString) {
toSerialize["array_of_string"] = o.ArrayOfString
}
for key, value := range o.AdditionalProperties {
toSerialize[key] = value
}
return toSerialize, nil
}
func (o *ArrayTestWithWrapped) UnmarshalJSON(data []byte) (err error) {
varArrayTestWithWrapped := _ArrayTestWithWrapped{}
err = json.Unmarshal(data, &varArrayTestWithWrapped)
if err != nil {
return err
}
*o = ArrayTestWithWrapped(varArrayTestWithWrapped)
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(data, &additionalProperties); err == nil {
delete(additionalProperties, "array_of_string")
o.AdditionalProperties = additionalProperties
}
return err
}
type NullableArrayTestWithWrapped struct {
value *ArrayTestWithWrapped
isSet bool
}
func (v NullableArrayTestWithWrapped) Get() *ArrayTestWithWrapped {
return v.value
}
func (v *NullableArrayTestWithWrapped) Set(val *ArrayTestWithWrapped) {
v.value = val
v.isSet = true
}
func (v NullableArrayTestWithWrapped) IsSet() bool {
return v.isSet
}
func (v *NullableArrayTestWithWrapped) Unset() {
v.value = nil
v.isSet = false
}
func NewNullableArrayTestWithWrapped(val *ArrayTestWithWrapped) *NullableArrayTestWithWrapped {
return &NullableArrayTestWithWrapped{value: val, isSet: true}
}
func (v NullableArrayTestWithWrapped) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
}
func (v *NullableArrayTestWithWrapped) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
}

View File

@@ -0,0 +1,155 @@
/*
OpenAPI Petstore
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
API version: 1.0.0
*/
// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.
package petstore
import (
"encoding/json"
)
// checks if the ArrayTestWithWrappedAndName type satisfies the MappedNullable interface at compile time
var _ MappedNullable = &ArrayTestWithWrappedAndName{}
// ArrayTestWithWrappedAndName struct for ArrayTestWithWrappedAndName
type ArrayTestWithWrappedAndName struct {
ArrayOfString []string `json:"array_of_string,omitempty"`
AdditionalProperties map[string]interface{}
}
type _ArrayTestWithWrappedAndName ArrayTestWithWrappedAndName
// NewArrayTestWithWrappedAndName instantiates a new ArrayTestWithWrappedAndName object
// This constructor will assign default values to properties that have it defined,
// and makes sure properties required by API are set, but the set of arguments
// will change when the set of required properties is changed
func NewArrayTestWithWrappedAndName() *ArrayTestWithWrappedAndName {
this := ArrayTestWithWrappedAndName{}
return &this
}
// NewArrayTestWithWrappedAndNameWithDefaults instantiates a new ArrayTestWithWrappedAndName object
// This constructor will only assign default values to properties that have it defined,
// but it doesn't guarantee that properties required by API are set
func NewArrayTestWithWrappedAndNameWithDefaults() *ArrayTestWithWrappedAndName {
this := ArrayTestWithWrappedAndName{}
return &this
}
// GetArrayOfString returns the ArrayOfString field value if set, zero value otherwise.
func (o *ArrayTestWithWrappedAndName) GetArrayOfString() []string {
if o == nil || IsNil(o.ArrayOfString) {
var ret []string
return ret
}
return o.ArrayOfString
}
// GetArrayOfStringOk returns a tuple with the ArrayOfString field value if set, nil otherwise
// and a boolean to check if the value has been set.
func (o *ArrayTestWithWrappedAndName) GetArrayOfStringOk() ([]string, bool) {
if o == nil || IsNil(o.ArrayOfString) {
return nil, false
}
return o.ArrayOfString, true
}
// HasArrayOfString returns a boolean if a field has been set.
func (o *ArrayTestWithWrappedAndName) HasArrayOfString() bool {
if o != nil && !IsNil(o.ArrayOfString) {
return true
}
return false
}
// SetArrayOfString gets a reference to the given []string and assigns it to the ArrayOfString field.
func (o *ArrayTestWithWrappedAndName) SetArrayOfString(v []string) {
o.ArrayOfString = v
}
func (o ArrayTestWithWrappedAndName) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {
return []byte{}, err
}
return json.Marshal(toSerialize)
}
func (o ArrayTestWithWrappedAndName) ToMap() (map[string]interface{}, error) {
toSerialize := map[string]interface{}{}
if !IsNil(o.ArrayOfString) {
toSerialize["array_of_string"] = o.ArrayOfString
}
for key, value := range o.AdditionalProperties {
toSerialize[key] = value
}
return toSerialize, nil
}
func (o *ArrayTestWithWrappedAndName) UnmarshalJSON(data []byte) (err error) {
varArrayTestWithWrappedAndName := _ArrayTestWithWrappedAndName{}
err = json.Unmarshal(data, &varArrayTestWithWrappedAndName)
if err != nil {
return err
}
*o = ArrayTestWithWrappedAndName(varArrayTestWithWrappedAndName)
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(data, &additionalProperties); err == nil {
delete(additionalProperties, "array_of_string")
o.AdditionalProperties = additionalProperties
}
return err
}
type NullableArrayTestWithWrappedAndName struct {
value *ArrayTestWithWrappedAndName
isSet bool
}
func (v NullableArrayTestWithWrappedAndName) Get() *ArrayTestWithWrappedAndName {
return v.value
}
func (v *NullableArrayTestWithWrappedAndName) Set(val *ArrayTestWithWrappedAndName) {
v.value = val
v.isSet = true
}
func (v NullableArrayTestWithWrappedAndName) IsSet() bool {
return v.isSet
}
func (v *NullableArrayTestWithWrappedAndName) Unset() {
v.value = nil
v.isSet = false
}
func NewNullableArrayTestWithWrappedAndName(val *ArrayTestWithWrappedAndName) *NullableArrayTestWithWrappedAndName {
return &NullableArrayTestWithWrappedAndName{value: val, isSet: true}
}
func (v NullableArrayTestWithWrappedAndName) MarshalJSON() ([]byte, error) {
return json.Marshal(v.value)
}
func (v *NullableArrayTestWithWrappedAndName) UnmarshalJSON(src []byte) error {
v.isSet = true
return json.Unmarshal(src, &v.value)
}