openapi-ts icon indicating copy to clipboard operation
openapi-ts copied to clipboard

External reference not resolved correctly

Open tde-desc opened this issue 1 year ago • 4 comments

Description

We have the following folder structure:

- openapi.json
- products
-- firstEndpoint.json
-- secondEndpoint.json
- schemas
-- FirstRequest.json
-- SecondRequest.json
-- SomeField.json

with:

openapi.json

{
  "openapi": "3.0.3",
  "info": {
    "title": "API",
    "version": "1.0.0"
  },
  "paths": {
    "/firstEndpoint": {
      "$ref": "products/firstEndpoint.json"
    },
    "/secondEndpoint": {
      "$ref": "products/secondEndpoint.json"
    }
  }
}

firstEndpoint.json

{
  "post": {
    "operationId": "testOne",
    "requestBody": {
      "content": {
        "application/json": {
          "schema": {
            "$ref": "schemas/FirstRequest.json"
          }
        }
      },
      "required": true
    },
    "responses": {
      "200": {
        "description": "OK"
      }
    }
  }
}

secondEndpoint.json

{
  "post": {
    "operationId": "testTwo",
    "requestBody": {
      "content": {
        "application/json": {
          "schema": {
            "$ref": "schemas/SecondRequest.json"
          }
        }
      },
      "required": true
    },
    "responses": {
      "200": {
        "description": "OK"
      }
    }
  }
}

FirstRequest.json

{
  "type": "object",
  "properties": {
    "someField": {
      "$ref": "SomeField.json"
    }
  }
}

SecondRequest.json

{
  "type": "object",
  "properties": {
    "someField": {
      "$ref": "SomeField.json"
    }
  }
}

SomeField.json

{
  "type": "object",
  "properties": {
    "someProperty": {
      "type": "string"
    }
  }
}

The problem is, that the types.gen.ts generates the first reference on SomeField.json in FirstRequest.json correctly but can not resolve the same reference in SecondRequest.json:

types.gen.ts

export type TestOneData = {
  requestBody: {
    someField?: {
      someProperty?: string;
    };
  };
};

export type TestTwoData = {
  requestBody: {
    someField?: paths__1firstEndpoint_post_requestBody_content_application_1json_schema_properties_someField; // <-- this does not compile
  };
};

OpenAPI specification (optional)

No response

Configuration

import { defineConfig } from '@hey-api/openapi-ts';

export default defineConfig({
  input: '../openapi/openapi-test.json',
  output: {
    path: 'src/client',
    format: 'prettier'
  },
  debug: true
});

System information (optional)

npm 10.7.0 Node 20.14.0 openapi-ts 0.46.3

tde-desc avatar Jun 10 '24 13:06 tde-desc

Interesting, thanks for reporting @tde-desc

mrlubos avatar Jun 10 '24 13:06 mrlubos

Do you have any idea, when you are going to address this issue? I am asking because we would like to use your lib intensively, but need a possibility to reuse external references.

tde-desc avatar Jun 21 '24 07:06 tde-desc

@tde-desc can you please create a reproducible example in StackBlitz?

mrlubos avatar Jun 22 '24 00:06 mrlubos

@mrlubos ofcause, here you go https://stackblitz.com/edit/stackblitz-starters-kowe2x?file=src%2Fclient%2Ftypes.gen.ts - reproducable with

npm run openapi-ts

tde-desc avatar Jun 22 '24 09:06 tde-desc

Duplicate of https://github.com/hey-api/openapi-ts/issues/1135

mrlubos avatar Nov 11 '24 01:11 mrlubos

cc @carson2222

mrlubos avatar Aug 27 '25 16:08 mrlubos

Hey @tde-desc, thanks for all your effort! I've managed to fix the ref parser issue: https://github.com/hey-api/json-schema-ref-parser/pull/10 We will soon update the openapi-ts to use the latest ref parser 1.0.7 version.

carson2222 avatar Aug 27 '25 17:08 carson2222