Skip to content

allOf: x-oapi-codegen-extra-tags leak to all properties that $ref an entity #1662

Description

@AhrazA

What version of ogen are you using?

v1.20.3

Can this issue be reproduced with the latest version?

Yes

What did you do?

openapi: "3.0.3"
info:
  title: ogen refcache tag leak repro
  version: "0.0.1"
paths:
  /foo:
    post:
      operationId: createFoo
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/Foo"
      responses:
        "200":
          description: ok
  /bar:
    get:
      operationId: getBar
      responses:
        "200":
          description: ok
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Bar"
components:
  schemas:
    SharedString:
      type: string

    # Parsed first — sets extra tags on the shared $ref pointer
    Foo:
      type: object
      properties:
        name:
          x-oapi-codegen-extra-tags:
            mytag: ignore
          $ref: "#/components/schemas/SharedString"

    # Parsed second — no extra tags, but gets them via mutated refcache
    Bar:
      type: object
      required: [name]
      properties:
        name:
          $ref: "#/components/schemas/SharedString"

What did you expect to see?

The generated struct for Bar should have no extra properties:

// Ref: #/components/schemas/Bar
type Bar struct {
	Name SharedString `json:"name"
}

What did you see instead?

The generated struct for Bar has mytag:"ignore" due to a leak from Foo.

// Ref: #/components/schemas/Bar
type Bar struct {
	Name SharedString `json:"name" mytag:"ignore"`
}

Workaround

To those facing this, we were able to workaround this issue by wrapping the $ref in allOf which prevents the leak:

    # Parsed first — sets extra tags, wrapped in allOf to avoid refcache mutation
    Foo:
      type: object
      properties:
        name:
          x-oapi-codegen-extra-tags:
            mytag: ignore
          allOf:
            - $ref: "#/components/schemas/SharedString"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions