-
-
Notifications
You must be signed in to change notification settings - Fork 530
Remote $ref used like in the Swagger documentation does not work #321
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
Comments
This should be fixed at least. But when generating schemas, what would you like to see the generated types become for this? |
Hey @drwpow, thats a good question. I'd say define a new interface? 🤔
|
Yeah I think that’s heading in the right direction. It‘ll be tricky to resolve, for sure, but possible. I think the right fix would be a lot of work, because it‘d involve resolving those files (and downloading them, as we currently support remote specs). So I want to split apart a short-term fix with a longer one. Short-term Future Scope However, if you kept everything inline, that would work for TS, but what if I‘ll spend a bit more time thinking on it, but here are 2 valid options I can think of off the top of my head:
interface components {
{ // …
"$ref": ExternalFiles["test.yaml"]["foo"]
}
}
interface ExternalFiles {
"test.yaml": {
foo: // …
}
}
} This would allow circular references back-and-forth, while being backwards-compatible with the current API. I think the only downside would be if you were trying to generate types for both specs, you‘d have duplication. But I think that‘d be expected, right? Because both files reference each other. WDYT about implementation? |
Hi @drwpow, thanks for your reply. I like the short term fix. This way the configuration would not throw. On the interface with the refs I'm not so sure. This way when we use this inside our code we would have to know the filename inside the OpenAPI project to use the type inside the programs code. I don't really know how the OpenAPI spec defines what should happen with circular references. From what I understand the $ref-block is 'replaced' by the corrosponding code - that would indeed break down pretty fast. It would be awesome to just be able to use that interface inside the code without having to use the components.$ref path. Maybe namespaces could be handy here? |
Oh you’re right. I made a mistake in my example. I should have illustrated that I think the rest of it may become more clear as we try and prototype this out. One unknown for me is how much of the imported file you can access; I’d probably lean toward “only what the original spec references specifically.” Anyways, I’ll get a quick fix out so the spec can generate, and will make a followup PR later for the longer-term fix (may take a while though). |
|
FYI there are libraries out there implementing the https://www.npmjs.com/package/@apidevtools/json-schema-ref-parser
It does resolve all references including external ones. |
@nponeccop that‘s a great find, thank you! I was not aware of namespaced schemas like this. If this is a good solution, then we could definitely add support for remote schemas in a future release. |
Since there are now quite a few requests for this, I’d love to add it. But there’s one problem: I think this would mean that the Node.js API for this library, which is currently synchronous, would have to be async. Because if it encounters remote schemas, it now has to fetch them (I know that if all This may cause a little disruption to some people using the Node.js API, but I think moving to async overall would be a win. CLI wouldn’t change. What are peoples’ thoughts? 👍🏻 or 👎🏻 ? |
IMO it would be fine to make it async-only and publish a breaking change. I will note that the most common use case for this package is in some sort of build step, which doesn't typically involve asynchronous operations. That being said I can't imagine it creating too big a lift for someone to update a few build scripts to async to support this. If we really need to keep backwards compatibility, we could alternatively provide two functions and log warn if a remote ref is encountered when using the sync version. |
My opinion is that we should use the same approach to async schemas that ajv.compile(schema: object): (data: any) => boolean | Promise < any > So we do sync validation whenever we can. But if it requires extra work, we could KISS by always doing async. Also, As for breaking changes, we can do it two ways:
|
Just wanted to give an update on this: I’m making progress on this feature and it’ll likely go out in the next minor release! Thanks to @nponeccop for the suggestion on json-schema-ref-parser. That was a big inspiration to unblocking this. I’ll have a PR up later this week that can act as a RFC for those interested. |
PR is up! Please take a look specifically at this test snapshot to get a feel for the newly-generated code: https://github.com/drwpow/openapi-typescript/pull/602/files#diff-884150d72ef4b660d8e83b0fcef66aeea1c8c1da9eddfebed53176ac43d4ebe7 |
Released this as Again, event though it’s a |
As seen here: https://swagger.io/docs/specification/using-ref/ JSON Reference supports remote references with an element selector.
Using this test setup:
index.yaml
test.yaml
I get the following error:
The text was updated successfully, but these errors were encountered: