openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

$ref: ./file.yaml: can't resolve components defined in a different file

Open krhubert opened this issue 2 years ago • 6 comments

Hey,

I want to use components that are defined in different file, but it's impossible to share let's say enum (or any other component). This makes $ref: files unusable (except to few top level fields like paths or componetns)

package.json

{
  "devDependencies": {
    "openapi-typescript-codegen": "^0.23.0"
  }
}

spec.yaml

openapi: 3.0.0
info:
  version: 0.0.1

paths:
  /a:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TypeA'

components:
  schemas:
    # I want to store component type per file
    # but it's impossible to use a component defined
    # in different file
    SharedEnumType:
      type: string
      enum: ['PENDING', 'DONE']

    TypeA:
      $ref: './type_a.yaml'

    TypeB:
      type: object
      properties:
        b:
          $ref: '#/components/schemas/SharedEnumType'

type_a.yaml

type: object
properties:
  a:
    $ref: '#/components/schemas/SharedEnumType' # works with type: string or if this is include in spec.yaml directly

cmd to run:

npx openapi -i spec.yaml -o gen -c axios

result:

  // ....
  code: 'EMISSINGPOINTER',
  message: 'Token "components" does not exist.',
  source: '/home/user/src/openapi-test/type_a.yaml',
  path: null,
  toJSON: [Function: toJSON],
  name: 'MissingPointerError',
  footprint: 'null+/home/user/src/openapi-test/type_a.yaml+EMISSINGPOINTER+Token "components" does not exist.',
  toString: [Function: toString]

krhubert avatar Aug 16 '22 12:08 krhubert

Any updates on this ? I am facing similar issue.

lubosnik avatar Jan 26 '23 14:01 lubosnik

Also facing this issue, any update or work around?

tiny-dancer avatar Jun 15 '23 14:06 tiny-dancer

I'm having the same problem.

joaoflaviosantos avatar Jul 14 '23 13:07 joaoflaviosantos

Same here. Version 0.25.0

derekdkim avatar Aug 14 '23 15:08 derekdkim

same issue 0.28.0

anatolii-yemets avatar Mar 25 '24 16:03 anatolii-yemets

@anatolii-yemets @derekdkim @joaoflaviosantos @tiny-dancer @lubosnik @krhubert Hey, I am taking a look at this issue. I would just say to check out @hey-api/openapi-ts which was originally a fork of this repository but will be maintained and updated (DISCLAIMER: I am a contributor).

Are you all managing your openapi specs manually? I have been able to modify the specs and get it working: spec.yaml:

openapi: 3.0.0
info:
  version: 0.0.1

paths:
  /a:
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TypeA'

components:
  schemas:
    # I want to store component type per file
    # but it's impossible to use a component defined
    # in different file
    SharedEnumType:
      type: string
      enum: ['PENDING', 'DONE']

    TypeA:
      $ref: 'type_a.yaml#/components/schemas/TypeA'

    TypeB:
      type: object
      properties:
        b:
          $ref: '#/components/schemas/SharedEnumType'

type_a.yaml:

components:
  schemas:
    TypeA:
      type: object
      properties:
        a:
          $ref: 'spec.yaml#/components/schemas/SharedEnumType'

I made these changes based on this documentation here. If you want to update to use our repository (@hey-api/openapi-ts) feel free to do so! and open any issues there or let me know if you are still running into this problem.

Thanks!

jordanshatford avatar Mar 25 '24 21:03 jordanshatford