diff --git a/internal/test/outputoptions/allow_lowercase_fields/config.yaml b/internal/test/outputoptions/allow_lowercase_fields/config.yaml new file mode 100644 index 000000000..7753605da --- /dev/null +++ b/internal/test/outputoptions/allow_lowercase_fields/config.yaml @@ -0,0 +1,9 @@ +# yaml-language-server: $schema=../../../../configuration-schema.json +package: allowlowercasefields +generate: + models: true +output: types.gen.go +output-options: + skip-prune: true + allow-unexported-fields: true + # TODO: SchemaNameToTypeName diff --git a/internal/test/outputoptions/allow_lowercase_fields/generate.go b/internal/test/outputoptions/allow_lowercase_fields/generate.go new file mode 100644 index 000000000..b1d54f40f --- /dev/null +++ b/internal/test/outputoptions/allow_lowercase_fields/generate.go @@ -0,0 +1,3 @@ +package allowlowercasefields + +//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml spec.yaml diff --git a/internal/test/outputoptions/allow_lowercase_fields/spec.yaml b/internal/test/outputoptions/allow_lowercase_fields/spec.yaml new file mode 100644 index 000000000..41fee4079 --- /dev/null +++ b/internal/test/outputoptions/allow_lowercase_fields/spec.yaml @@ -0,0 +1,11 @@ +components: + schemas: + my_item: + type: object + properties: + name: + type: string + age: + type: integer + # NOTE that this is an unexported field + x-go-name: age diff --git a/internal/test/outputoptions/allow_lowercase_fields/types.gen.go b/internal/test/outputoptions/allow_lowercase_fields/types.gen.go new file mode 100644 index 000000000..64d8b95cd --- /dev/null +++ b/internal/test/outputoptions/allow_lowercase_fields/types.gen.go @@ -0,0 +1,10 @@ +// Package allowlowercasefields provides primitives to interact with the openapi HTTP API. +// +// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.0.0-00010101000000-000000000000 DO NOT EDIT. +package allowlowercasefields + +// MyItem defines model for my_item. +type MyItem struct { + Age *int `json:"age,omitempty"` + Name *string `json:"name,omitempty"` +} diff --git a/internal/test/outputoptions/allow_lowercase_fields/types_test.go b/internal/test/outputoptions/allow_lowercase_fields/types_test.go new file mode 100644 index 000000000..409501fcd --- /dev/null +++ b/internal/test/outputoptions/allow_lowercase_fields/types_test.go @@ -0,0 +1,10 @@ +package allowlowercasefields + +import "testing" + +func TestMyItemCompiles(t *testing.T) { + _ = MyItem{ + Name: "a string", + age: 1_000, + } +} diff --git a/pkg/codegen/configuration.go b/pkg/codegen/configuration.go index 0ada42f0a..df6117225 100644 --- a/pkg/codegen/configuration.go +++ b/pkg/codegen/configuration.go @@ -139,6 +139,8 @@ type OutputOptions struct { // NameNormalizer is the method used to normalize Go names and types, for instance converting the text `MyApi` to `MyAPI`. Corresponds with the constants defined for `codegen.NameNormalizerFunction` NameNormalizer string `yaml:"name-normalizer,omitempty"` + + AllowUnexportedFields bool `yaml:"allow-unexported-fields"` } // UpdateDefaults sets reasonable default values for unset fields in Configuration diff --git a/pkg/codegen/schema.go b/pkg/codegen/schema.go index f734517a9..8cd7f57e2 100644 --- a/pkg/codegen/schema.go +++ b/pkg/codegen/schema.go @@ -100,6 +100,8 @@ func (p Property) GoFieldName() string { } } + // return goFieldName + return SchemaNameToTypeName(goFieldName) } @@ -664,6 +666,7 @@ func GenFieldsFromProperties(props []Property) []string { goFieldName := p.GoFieldName() + fmt.Printf("goFieldName: %v\n", goFieldName) // Add a comment to a field in case we have one, otherwise skip. if p.Description != "" { // Separate the comment from a previous-defined, unrelated field. diff --git a/pkg/codegen/utils.go b/pkg/codegen/utils.go index b955e5290..1990d6115 100644 --- a/pkg/codegen/utils.go +++ b/pkg/codegen/utils.go @@ -226,6 +226,10 @@ func ToCamelCase(str string) string { n := "" capNext := true + // if globalState.options.OutputOptions.AllowUnexportedFields { + // capNext = false + // } + for _, v := range s { if unicode.IsUpper(v) { n += string(v)