oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Unexpected reference depth

Open ghost opened this issue 2 years ago • 6 comments

Issue

Error occurs when $ref refers to a property of another object.

This feature can be useful because it allows you to edit the properties of an object in one place (especially when using different models for requests).

Example

openapi: 3.0.3
info:
  title: test-service
  version: 1.0.0

paths:
  /samples:
    post:
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/CreateSample"
      responses:
        201:
          description: ".."
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Sample"

components:
  schemas:
    Sample:
      type: object
      properties:
        id:
          type: string
          format: uuid
        field:
          type: string
          # more properties here

    CreateSample:
      type: object
      properties:
        field:
          $ref: "#/components/schemas/Sample/properties/field"

Output

Error generating code: error generating type definitions: error generating Go types for component schemas: error converting Schema CreateSample to Go type: error generating Go schema for property 'field': error turning reference (#/components/schemas/Sample/properties/field) into a Go type: unexpected reference depth: 6 for ref: #/components/schemas/Sample/properties/field local: true

ghost avatar Nov 03 '23 10:11 ghost

Interesting! I think this isn't a use-case we expected, but I can see why it's useful.

I wonder if a fix for this is best gated behind a flag to allow folks to opt-in to the changes, but I'd be happy with there being support added for this, and would review a PR that's raised for it!

jamietanna avatar Dec 13 '23 12:12 jamietanna

Any updates on this? Facing the same issue and don't really know what it's about

bjuan210302 avatar Mar 03 '24 04:03 bjuan210302

This may now be closed by https://github.com/oapi-codegen/oapi-codegen/pull/1689 and some follow up changes - would it be possible for someone affected to validate?

jamietanna avatar Jul 30 '24 15:07 jamietanna