[Go] Serialize readonly fields (#15765)

* Do not skip readOnly fields in ToMap()

* Regenerate sample

* Add test

* Correct test
This commit is contained in:
Beppe Catanese 2023-06-10 13:23:52 +02:00 committed by GitHub
parent 1fb0ab997f
commit f240ed4e4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 52 additions and 38 deletions

View File

@ -311,7 +311,6 @@ func (o {{classname}}) ToMap() (map[string]interface{}, error) {
{{/isNullable}}
{{! if argument is not nullable, don't set it if it is nil}}
{{^isNullable}}
{{^isReadOnly}}
{{#required}}
toSerialize["{{baseName}}"] = o.{{name}}
{{/required}}
@ -320,10 +319,6 @@ func (o {{classname}}) ToMap() (map[string]interface{}, error) {
toSerialize["{{baseName}}"] = o.{{name}}
}
{{/required}}
{{/isReadOnly}}
{{#isReadOnly}}
// skip: {{baseName}} is readOnly
{{/isReadOnly}}
{{/isNullable}}
{{/vars}}
{{#isAdditionalPropertiesTrue}}

View File

@ -291,23 +291,4 @@ public class GoClientCodegenTest {
"httpRes, err := apiClient.PetAPI.PetDelete(context.Background()).Execute()");
}
@Test
public void verifyReadOnlyAttributes() throws IOException {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("go")
.setInputSpec("src/test/resources/3_0/property-readonly.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
files.forEach(File::deleteOnExit);
TestUtils.assertFileExists(Paths.get(output + "/model_request.go"));
TestUtils.assertFileContains(Paths.get(output + "/model_request.go"),
"// skip: customerCode is readOnly");
}
}

View File

@ -114,8 +114,12 @@ func (o HasOnlyReadOnly) MarshalJSON() ([]byte, error) {
func (o HasOnlyReadOnly) ToMap() (map[string]interface{}, error) {
toSerialize := map[string]interface{}{}
// skip: bar is readOnly
// skip: foo is readOnly
if !IsNil(o.Bar) {
toSerialize["bar"] = o.Bar
}
if !IsNil(o.Foo) {
toSerialize["foo"] = o.Foo
}
return toSerialize, nil
}

View File

@ -174,11 +174,15 @@ func (o Name) MarshalJSON() ([]byte, error) {
func (o Name) ToMap() (map[string]interface{}, error) {
toSerialize := map[string]interface{}{}
toSerialize["name"] = o.Name
// skip: snake_case is readOnly
if !IsNil(o.SnakeCase) {
toSerialize["snake_case"] = o.SnakeCase
}
if !IsNil(o.Property) {
toSerialize["property"] = o.Property
}
// skip: 123Number is readOnly
if !IsNil(o.Var123Number) {
toSerialize["123Number"] = o.Var123Number
}
return toSerialize, nil
}

View File

@ -114,7 +114,9 @@ func (o ReadOnlyFirst) MarshalJSON() ([]byte, error) {
func (o ReadOnlyFirst) ToMap() (map[string]interface{}, error) {
toSerialize := map[string]interface{}{}
// skip: bar is readOnly
if !IsNil(o.Bar) {
toSerialize["bar"] = o.Bar
}
if !IsNil(o.Baz) {
toSerialize["baz"] = o.Baz
}

View File

@ -117,8 +117,12 @@ func (o HasOnlyReadOnly) MarshalJSON() ([]byte, error) {
func (o HasOnlyReadOnly) ToMap() (map[string]interface{}, error) {
toSerialize := map[string]interface{}{}
// skip: bar is readOnly
// skip: foo is readOnly
if !IsNil(o.Bar) {
toSerialize["bar"] = o.Bar
}
if !IsNil(o.Foo) {
toSerialize["foo"] = o.Foo
}
for key, value := range o.AdditionalProperties {
toSerialize[key] = value

View File

@ -177,11 +177,15 @@ func (o Name) MarshalJSON() ([]byte, error) {
func (o Name) ToMap() (map[string]interface{}, error) {
toSerialize := map[string]interface{}{}
toSerialize["name"] = o.Name
// skip: snake_case is readOnly
if !IsNil(o.SnakeCase) {
toSerialize["snake_case"] = o.SnakeCase
}
if !IsNil(o.Property) {
toSerialize["property"] = o.Property
}
// skip: 123Number is readOnly
if !IsNil(o.Var123Number) {
toSerialize["123Number"] = o.Var123Number
}
for key, value := range o.AdditionalProperties {
toSerialize[key] = value

View File

@ -117,7 +117,9 @@ func (o ReadOnlyFirst) MarshalJSON() ([]byte, error) {
func (o ReadOnlyFirst) ToMap() (map[string]interface{}, error) {
toSerialize := map[string]interface{}{}
// skip: bar is readOnly
if !IsNil(o.Bar) {
toSerialize["bar"] = o.Bar
}
if !IsNil(o.Baz) {
toSerialize["baz"] = o.Baz
}

View File

@ -294,16 +294,24 @@ func (o ReadOnlyWithDefault) MarshalJSON() ([]byte, error) {
func (o ReadOnlyWithDefault) ToMap() (map[string]interface{}, error) {
toSerialize := map[string]interface{}{}
// skip: prop1 is readOnly
// skip: prop2 is readOnly
if !IsNil(o.Prop1) {
toSerialize["prop1"] = o.Prop1
}
if !IsNil(o.Prop2) {
toSerialize["prop2"] = o.Prop2
}
if !IsNil(o.Prop3) {
toSerialize["prop3"] = o.Prop3
}
// skip: boolProp1 is readOnly
if !IsNil(o.BoolProp1) {
toSerialize["boolProp1"] = o.BoolProp1
}
if !IsNil(o.BoolProp2) {
toSerialize["boolProp2"] = o.BoolProp2
}
// skip: intProp1 is readOnly
if !IsNil(o.IntProp1) {
toSerialize["intProp1"] = o.IntProp1
}
if !IsNil(o.IntProp2) {
toSerialize["intProp2"] = o.IntProp2
}

View File

@ -41,3 +41,13 @@ func TestDog(t *testing.T) {
//assert.Nil(newDog)
assert.Equal(*newDog.Breed, breedString, "Breed should be `Shepherd`")
}
func TestReadOnlyFirst(t *testing.T) {
assert := assert.New(t)
newReadOnlyFirst := (sw.ReadOnlyFirst{Bar: sw.PtrString("Bar value"), Baz: sw.PtrString("Baz value")})
json, _ := newReadOnlyFirst.MarshalJSON()
expected := `{"bar":"Bar value","baz":"Baz value"}`
assert.Equal(expected, (string)(json), "ReadOnlyFirst JSON is incorrect")
}