diff --git a/pkg/codegen/configuration.go b/pkg/codegen/configuration.go index 727d813ff..290c139c3 100644 --- a/pkg/codegen/configuration.go +++ b/pkg/codegen/configuration.go @@ -57,6 +57,10 @@ type CompatibilityOptions struct { // When an object contains no members, and only an additionalProperties specification, // it is flattened to a map. Set DisableFlattenAdditionalProperties bool `yaml:"disable-flatten-additional-properties,omitempty"` + // When an object property is both required and readOnly the go model is generated + // as a pointer. Set DisableRequiredReadOnlyAsPointer to true to mark them as non pointer. + // Please see https://github.com/deepmap/oapi-codegen/issues/604 + DisableRequiredReadOnlyAsPointer bool `yaml:"disable-required-readonly-as-pointer,omitempty"` // When set to true, always prefix enum values with their type name instead of only // when typenames would be conflicting. AlwaysPrefixEnumValues bool `yaml:"always-prefix-enum-values,omitempty"` diff --git a/pkg/codegen/schema.go b/pkg/codegen/schema.go index ce5f586a8..afb07eb2c 100644 --- a/pkg/codegen/schema.go +++ b/pkg/codegen/schema.go @@ -94,7 +94,9 @@ func (p Property) GoFieldName() string { func (p Property) GoTypeDef() string { typeDef := p.Schema.TypeDecl() if !p.Schema.SkipOptionalPointer && - (!p.Required || p.Nullable || p.ReadOnly || p.WriteOnly) { + (!p.Required || p.Nullable || + (p.ReadOnly && (!p.Required || !globalState.options.Compatibility.DisableRequiredReadOnlyAsPointer)) || + p.WriteOnly) { typeDef = "*" + typeDef } @@ -594,7 +596,7 @@ func GenFieldsFromProperties(props []Property) []string { fieldTags := make(map[string]string) - if (p.Required && !p.ReadOnly && !p.WriteOnly) || p.Nullable || !overrideOmitEmpty { + if (p.Required && !p.ReadOnly && !p.WriteOnly) || p.Nullable || !overrideOmitEmpty || (p.Required && p.ReadOnly && globalState.options.Compatibility.DisableRequiredReadOnlyAsPointer) { fieldTags["json"] = p.JsonFieldName if p.NeedsFormTag { fieldTags["form"] = p.JsonFieldName