Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When there is a allOf, the $ref are never resolved #2

Open
joepjoosten opened this issue Jun 27, 2024 · 2 comments
Open

When there is a allOf, the $ref are never resolved #2

joepjoosten opened this issue Jun 27, 2024 · 2 comments

Comments

@joepjoosten
Copy link

I'm parsing the spec for opensearch (https://github.com/opensearch-project/opensearch-api-specification/releases/download/main-latest/opensearch-openapi.yaml)

To download it, and transform it to json:

curl -L https://github.com/opensearch-project/opensearch-api-specification/releases/download/main-latest/opensearch-openapi.yaml > opensearch-openapi.yaml \
| yq -o=json eval opensearch-openapi.yaml > opensearch-openapi.json

When there is an allOf which has a $ref, it always fails because it can't find the schema.

Error:

Error: Unable to find schema "#/components/schemas/_common:WriteResponseBase"
    at isObjectSchema (file:///.//openapi-to-effect/dist/esm/analysis/GraphAnalyzer.mjs:268:11)
    at file:///.//openapi-to-effect/dist/esm/generation/effSchemGen/schemaGen.mjs:327:27
    at Array.reduce (<anonymous>)
    at generateForSchema (file:///.//openapi-to-effect/dist/esm/generation/effSchemGen/schemaGen.mjs:325:44)
    at generateModule (file:///.//openapi-to-effect/dist/esm/generation/effSchemGen/moduleGen.mjs:28:7)
    at file:///.//openapi-to-effect/dist/esm/openapiToEffect.mjs:106:18
    at generateSchemas (file:///.//openapi-to-effect/dist/esm/openapiToEffect.mjs:111:9)
    at async runGenerator (file:///.//openapi-to-effect/dist/esm/openapiToEffect.mjs:405:5)
    at async file:///.//openapi-to-effect/dist/esm/openapiToEffect.mjs:473:9
    at async run (file:///.//openapi-to-effect/dist/esm/openapiToEffect.mjs:459:3)

Looks like the isObjectSchema function in GraphAnalyzer.ts is always empty

@mkrause
Copy link
Collaborator

mkrause commented Jun 27, 2024

@joepjoosten Tracked it down to this FIXME:

export const generateModule = (schemaId: string, schema: OpenApiSchema): string => {
  const generationContext: SchemaGenContext = {
    schemas: {}, // FIXME
    hooks: {},
    isSchemaIdBefore(_schemaId: OpenApiSchemaId) { return true; },
  };

For the default one-file-per-schema it doesn't yet have enough context here to resolve references to other schemas.

However, I went though the opensearch API you posted, and got it working with the single bundle mode, with a few fixes in this PR (tagged as v0.7.1):

#3

You'd need to pass in the following spec using the --spec option:

export default {
  generationMethod: { method: 'bundled', bundleName: 'opensearch' },
  hooks: {},
  runtime: {},
  modules: {},
};

The output is:

https://gist.github.com/mkrause/b85e025d1fc1b5dd2bd2f4b522af2dae

There's still a number of type errors in the resulting bundle however, which are due to recursive schema references. You can add manual type annotations for these in the spec, like in the example in the README here.

@mkrause
Copy link
Collaborator

mkrause commented Jun 27, 2024

Adding a whole bunch of manual type declarations is obviously not very convenient, and I'm considering adding some TypeScript type generation for the schemas that have recursive references. Unfortunately it's not always straightforward to find the right type to fix a recursive type reference, hence why it's a manual step currently. But it should be possible to automate most cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants