12551 [Go] Skipping marshalling of readonly fields (#14335)

* Skip toSerialize for readOnly fields

* Commit regenerated files
This commit is contained in:
Beppe Catanese 2023-01-02 16:35:54 +01:00 committed by GitHub
parent 921199bba7
commit d0800c46e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 83 additions and 42 deletions

View File

@ -311,6 +311,7 @@ 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}}
@ -319,6 +320,10 @@ 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

@ -286,4 +286,24 @@ public class GoClientCodegenTest {
TestUtils.assertFileContains(Paths.get(output + "/test/api_pet_test.go"),
"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

@ -0,0 +1,44 @@
openapi: 3.0.1
info:
version: 1.0.0
title: Example
license:
name: MIT
servers:
- url: http://api.example.xyz/v1
paths:
/test:
post:
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Request'
responses:
'200':
description: responses
content:
application/json:
schema:
$ref: '#/components/schemas/Response'
components:
schemas:
Request:
type: object
properties:
customerCode:
type: string
readOnly: true
example: '0001'
firstName:
type: string
example: 'first'
Response:
type: object
properties:
customerCode:
type: string
example: '0001'
firstName:
type: string
example: 'first'

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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