Skip to content

openapi3gen: skip component export for anonymous types#1163

Merged
fenollp merged 1 commit into
getkin:masterfrom
0-don:fix/skip-anonymous-component-export
May 9, 2026
Merged

openapi3gen: skip component export for anonymous types#1163
fenollp merged 1 commit into
getkin:masterfrom
0-don:fix/skip-anonymous-component-export

Conversation

@0-don

@0-don 0-don commented May 3, 2026

Copy link
Copy Markdown
Contributor

Problem

When ExportComponentSchemas: true is set and the generator walks into a struct field whose type is anonymous (e.g. an inline struct{...} literal), reflect.Type.Name() returns "". The component-export branch in generateSchemaRefFor happily registers it:

typeName := g.generateTypeName(t)
g.componentSchemaRefs[typeName] = struct{}{}
return openapi3.NewSchemaRef(fmt.Sprintf("#/components/schemas/%s", typeName), schema), nil

The result is a component keyed by "" and a \$ref of #/components/schemas/. Both violate the OpenAPI 3 component-key pattern (^[a-zA-Z0-9._-]+\$) and downstream codegen tools reject the spec:

🛑 Invalid component key found. OpenAPI component keys must match the pattern /^[a-zA-Z0-9.\-_]+/
  Invalid keys:
    - components.schemas.

Fix

When generateTypeName returns "", inline the schema instead of registering it as a component. This matches the behavior already used for generics and time.Time in the same function.

Test

TestExportComponentSchemasSkipsAnonymousType registers a struct with an inline anonymous field and asserts no component schema is created with an empty key.

Comment thread openapi3gen/openapi3gen_test.go
Anonymous nested struct types have an empty reflect.Type.Name(), so
generateTypeName returns "". Registering them under that key produces
"#/components/schemas/" refs that violate the OpenAPI spec (component
keys must match ^[a-zA-Z0-9._-]+$) and break codegen tools like Orval.
Inline the schema instead, matching the existing behavior for generics
and time.Time.
@0-don 0-don force-pushed the fix/skip-anonymous-component-export branch from 0ccd467 to 9cfa6b7 Compare May 9, 2026 18:31
@fenollp fenollp merged commit 94286d9 into getkin:master May 9, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants