diff --git a/modules/openapi-generator/src/main/resources/go/model_simple.mustache b/modules/openapi-generator/src/main/resources/go/model_simple.mustache index 05e56b11b33..d634c704a34 100644 --- a/modules/openapi-generator/src/main/resources/go/model_simple.mustache +++ b/modules/openapi-generator/src/main/resources/go/model_simple.mustache @@ -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}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java index a4b23a4cf11..ce22ca83f46 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java @@ -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 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"); - } - } diff --git a/samples/client/petstore/go/go-petstore/model_has_only_read_only.go b/samples/client/petstore/go/go-petstore/model_has_only_read_only.go index 7fd71a40d34..fd4c4c68619 100644 --- a/samples/client/petstore/go/go-petstore/model_has_only_read_only.go +++ b/samples/client/petstore/go/go-petstore/model_has_only_read_only.go @@ -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 } diff --git a/samples/client/petstore/go/go-petstore/model_name.go b/samples/client/petstore/go/go-petstore/model_name.go index 76b59209ac4..e1fe302008f 100644 --- a/samples/client/petstore/go/go-petstore/model_name.go +++ b/samples/client/petstore/go/go-petstore/model_name.go @@ -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 } diff --git a/samples/client/petstore/go/go-petstore/model_read_only_first.go b/samples/client/petstore/go/go-petstore/model_read_only_first.go index c5951776226..fb7df05bff1 100644 --- a/samples/client/petstore/go/go-petstore/model_read_only_first.go +++ b/samples/client/petstore/go/go-petstore/model_read_only_first.go @@ -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 } diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_has_only_read_only.go b/samples/openapi3/client/petstore/go/go-petstore/model_has_only_read_only.go index 42a5caae896..7f37585fa2e 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_has_only_read_only.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_has_only_read_only.go @@ -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 diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_name.go b/samples/openapi3/client/petstore/go/go-petstore/model_name.go index 934e03ff549..74b7b5deda0 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_name.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_name.go @@ -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 diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_read_only_first.go b/samples/openapi3/client/petstore/go/go-petstore/model_read_only_first.go index 0b0334c30f3..6aa66a79f20 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_read_only_first.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_read_only_first.go @@ -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 } diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_read_only_with_default.go b/samples/openapi3/client/petstore/go/go-petstore/model_read_only_with_default.go index 90522780d32..146b94ebc14 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_read_only_with_default.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_read_only_with_default.go @@ -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 } diff --git a/samples/openapi3/client/petstore/go/model_test.go b/samples/openapi3/client/petstore/go/model_test.go index 108a961eb1b..34868a43fdb 100644 --- a/samples/openapi3/client/petstore/go/model_test.go +++ b/samples/openapi3/client/petstore/go/model_test.go @@ -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") +}