Skip to content

Commit f71a97a

Browse files
percivalalbAlex Barter
authored andcommitted
Reconcile matching schemas in root document to avoid anonymous structs
1 parent 5e33981 commit f71a97a

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

internal/test/issues/issue-1362/issue1362.gen.go

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/test/issues/issue-1572/issue1572.gen.go

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/codegen/codegen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ func GenerateTypesForSchemas(t *template.Template, schemas map[string]*openapi3.
521521
}
522522
schemaRef := schemas[schemaName]
523523

524-
goSchema, err := GenerateGoSchema(schemaRef, []string{schemaName})
524+
goSchema, err := GenerateGoSchema(schemaRef, []string{schemaName}, true)
525525
if err != nil {
526526
return nil, fmt.Errorf("error converting Schema %s to Go type: %w", schemaName, err)
527527
}

pkg/codegen/schema.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func PropertiesEqual(a, b Property) bool {
232232
return a.JsonFieldName == b.JsonFieldName && a.Schema.TypeDecl() == b.Schema.TypeDecl() && a.Required == b.Required
233233
}
234234

235-
func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
235+
func GenerateGoSchema(sref *openapi3.SchemaRef, path []string, fixedField ...bool) (Schema, error) {
236236
// Add a fallback value in case the sref is nil.
237237
// i.e. the parent schema defines a type:array, but the array has
238238
// no items defined. Therefore, we have at least valid Go-Code.
@@ -242,6 +242,26 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
242242

243243
schema := sref.Value
244244

245+
// Uses a vardic to maintain the same function signature and to avoid updating all invocations.
246+
// Defaults to fixedField = false
247+
if len(fixedField) == 0 || !fixedField[0] {
248+
// If the reference is the same as one in the schema components fixed fields use the same
249+
// type to avoid generation of anonymous structures as much as possible.
250+
if refPath, match := openapi3.ReferencesComponentInRootDocument(globalState.spec, sref); match {
251+
refType, err := RefPathToGoType(refPath)
252+
if err != nil {
253+
return Schema{}, fmt.Errorf("error turning reference (%s) matched %s into a Go type: %s",
254+
sref.Ref, refPath, err)
255+
}
256+
return Schema{
257+
GoType: refType,
258+
Description: schema.Description,
259+
DefineViaAlias: true,
260+
OAPISchema: schema,
261+
}, nil
262+
}
263+
}
264+
245265
// If Ref is set on the SchemaRef, it means that this type is actually a reference to
246266
// another type. We're not de-referencing, so simply use the referenced type.
247267
if IsGoTypeReference(sref.Ref) {

0 commit comments

Comments
 (0)