From d07ec12d69d3db0e5b502be7528884fdb5fb7593 Mon Sep 17 00:00:00 2001 From: Ian Date: Sat, 11 Jun 2022 09:17:01 -0700 Subject: [PATCH] Use exact capitalization from field names overridden in config (#2237) --- plugin/modelgen/models.go | 14 +++++++++----- plugin/modelgen/models.gotpl | 2 +- plugin/modelgen/models_test.go | 4 ++++ plugin/modelgen/out/generated.go | 5 +++++ plugin/modelgen/out_struct_pointers/generated.go | 5 +++++ plugin/modelgen/testdata/gqlgen.yml | 4 ++++ plugin/modelgen/testdata/schema.graphql | 5 +++++ 7 files changed, 33 insertions(+), 6 deletions(-) diff --git a/plugin/modelgen/models.go b/plugin/modelgen/models.go index 9d0db43afcb..110b0bc3bd8 100644 --- a/plugin/modelgen/models.go +++ b/plugin/modelgen/models.go @@ -48,9 +48,12 @@ type Object struct { type Field struct { Description string - Name string - Type types.Type - Tag string + // Name is the field's name as it appears in the schema + Name string + // GoName is the field's name as it appears in the generated Go code + GoName string + Type types.Type + Tag string } type Enum struct { @@ -178,7 +181,7 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error { } } - name := field.Name + name := templates.ToGo(field.Name) if nameOveride := cfg.Models[schemaType.Name].Fields[field.Name].FieldName; nameOveride != "" { name = nameOveride } @@ -192,7 +195,8 @@ func (m *Plugin) MutateConfig(cfg *config.Config) error { } f := &Field{ - Name: name, + Name: field.Name, + GoName: name, Type: typ, Description: field.Description, Tag: `json:"` + field.Name + `"`, diff --git a/plugin/modelgen/models.gotpl b/plugin/modelgen/models.gotpl index 8f425e58eb4..413ddba9e96 100644 --- a/plugin/modelgen/models.gotpl +++ b/plugin/modelgen/models.gotpl @@ -29,7 +29,7 @@ {{- with .Description }} {{.|prefixLines "// "}} {{- end}} - {{ $field.Name|go }} {{$field.Type | ref}} `{{$field.Tag}}` + {{ $field.GoName }} {{$field.Type | ref}} `{{$field.Tag}}` {{- end }} } diff --git a/plugin/modelgen/models_test.go b/plugin/modelgen/models_test.go index e6e4f09cbd9..12e24c87649 100644 --- a/plugin/modelgen/models_test.go +++ b/plugin/modelgen/models_test.go @@ -236,6 +236,10 @@ func TestModelGeneration(t *testing.T) { require.Nil(t, out.Recursive{}.FieldThree) require.NotNil(t, out.Recursive{}.FieldFour) }) + + t.Run("overridden struct field names use same capitalization as config", func(t *testing.T) { + require.NotNil(t, out.RenameFieldTest{}.GOODnaME) + }) } func TestModelGenerationStructFieldPointers(t *testing.T) { diff --git a/plugin/modelgen/out/generated.go b/plugin/modelgen/out/generated.go index e57ce6471cb..1445f04eda6 100644 --- a/plugin/modelgen/out/generated.go +++ b/plugin/modelgen/out/generated.go @@ -131,6 +131,11 @@ type Recursive struct { FieldFour string `json:"FieldFour" database:"RecursiveFieldFour"` } +type RenameFieldTest struct { + GOODnaME string `json:"badName" database:"RenameFieldTestbadName"` + OtherField string `json:"otherField" database:"RenameFieldTestotherField"` +} + // TypeWithDescription is a type with a description type TypeWithDescription struct { Name *string `json:"name" database:"TypeWithDescriptionname"` diff --git a/plugin/modelgen/out_struct_pointers/generated.go b/plugin/modelgen/out_struct_pointers/generated.go index c84c31f388e..373376ef0d4 100644 --- a/plugin/modelgen/out_struct_pointers/generated.go +++ b/plugin/modelgen/out_struct_pointers/generated.go @@ -131,6 +131,11 @@ type Recursive struct { FieldFour string `json:"FieldFour" database:"RecursiveFieldFour"` } +type RenameFieldTest struct { + BadName string `json:"badName" database:"RenameFieldTestbadName"` + OtherField string `json:"otherField" database:"RenameFieldTestotherField"` +} + // TypeWithDescription is a type with a description type TypeWithDescription struct { Name *string `json:"name" database:"TypeWithDescriptionname"` diff --git a/plugin/modelgen/testdata/gqlgen.yml b/plugin/modelgen/testdata/gqlgen.yml index fcc8b7163a4..37db711cef2 100644 --- a/plugin/modelgen/testdata/gqlgen.yml +++ b/plugin/modelgen/testdata/gqlgen.yml @@ -19,4 +19,8 @@ models: model: github.com/99designs/gqlgen/plugin/modelgen/out.ExistingUnion ExistingType: model: github.com/99designs/gqlgen/plugin/modelgen/out.ExistingType + RenameFieldTest: + fields: + badName: + fieldName: GOODnaME diff --git a/plugin/modelgen/testdata/schema.graphql b/plugin/modelgen/testdata/schema.graphql index 23fd26ced81..3a99ee168ef 100644 --- a/plugin/modelgen/testdata/schema.graphql +++ b/plugin/modelgen/testdata/schema.graphql @@ -164,3 +164,8 @@ type Recursive { FieldThree: Recursive! FieldFour: String! } + +type RenameFieldTest { + badName: String! + otherField: String! +}