`generate-types-for-anonymous-schemas` generates invalid Go identifiers for numeric response keys

Author: ryymCreated Sep 16, 2026Updated Sep 16, 2026

Hello, thank you for creating this tool!

What happened?

I'm excited about the new option output-options.generate-types-for-anonymous-schemas added in v2.8.0. But with enabling it, generating code from a spec whose responses keys are status codes (e.g. '400') produces invalid Go identifiers that start with a digit. So the generated code fails to compile.

The top-level response type is named correctly like N400, but the hoisted type for a inline schema comes out like 400_Errors.

What did you expect to happen?

The nested hoisted type should also be a valid Go identifier (e.g. N400_Errors), so the generated code compiles.

Minimal reproduction

oapi-codegen version: v2.7.1 (also reproduced on main at 2679cec6)

Go version: go1.27.1 darwin/arm64

config.yaml

yaml
package: repro
generate:
  models: true
output: model_gen.go
output-options:
  generate-types-for-anonymous-schemas: true

spec.yaml

yaml
openapi: 3.0.0
info:
  title: repro
  version: 0.0.1
paths:
  /foo:
    post:
      operationId: createFoo
      responses:
        "201":
          description: success
        "400":
          $ref: "#/components/responses/400"
components:
  responses:
    "400":
      description: Bad Request
      content:
        application/json:
          schema:
            type: object
            properties:
              errors:
                type: array
                items:
                  type: object
                  properties:
                    message:
                      type: string
                  required:
                    - message
            required:
              - errors

Command

bash
go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen -config ./config.yaml spec.yaml

Output

error generating code: error formatting Go code at line 39: repro.go:39:17: '_' must separate successive digits (and 3 more errors)
exit status 1

The generated code:

go
// Package repro provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version (devel) DO NOT EDIT.
package repro

// import (...)

// N400 defines model for 400.
type N400  struct {
    Errors []400_Errors`json:"errors"`
}

// 400_Errors defines model for 400.Errors.
type 400_Errors  struct {
    Message string`json:"message"`
}



// CreateFoo400JSONResponseBody_Errors defines parameters for CreateFoo.
type CreateFoo400JSONResponseBody_Errors  struct {
    Message string`json:"message"`
}

400_Errors is not a valid Go identifier, so gofmt fails and codegen aborts.

I'm also not sure what CreateFoo400JSONResponseBody_Errors is. It is not generated with generate-types-for-anonymous-schemas: false.

Source: oapi-codegen/oapi-codegen