-
-
Notifications
You must be signed in to change notification settings - Fork 159
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
OpenAPI: Support including related resources #1448
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…o there's no need.
…esourceIdentifier -> Identifier, drop Object suffixes
… provider, remove ISchemaRepositoryAccessor, remove OAS caching (no measurable gain)
bkoelman
force-pushed
the
openapi-includes
branch
from
February 2, 2024 00:37
38c8d0b
to
e46cdff
Compare
bkoelman
force-pushed
the
openapi-includes
branch
from
February 2, 2024 00:40
ecf3a3c
to
9899afa
Compare
…by client tests; expand model used for query strings
bkoelman
force-pushed
the
openapi-includes
branch
4 times, most recently
from
February 2, 2024 02:11
b2ee92b
to
24123dd
Compare
…sk (makes it easier to keep in sync during development)
…with discriminator
bkoelman
force-pushed
the
openapi-includes
branch
from
February 2, 2024 02:54
24123dd
to
97bd1bc
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds OAS support for the top-level "included" array, which is used in JSON:API responses. You get them when using the
include
query string parameter.As I'm getting more familiar with the OpenAPI code originally written by @maurei, I found opportunities for improving consistency, simplifying things, applying corrections, and fixing some bugs. These are part of the PR as well, see the individual commits for details.
An example is often easier to understand, so let's see what's changed from a usage perspective.
snippet: allOf inheritance in swagger.json
In the snippet above, observe that:
dataInResponse
(newly added) defines the base schema for included resources (which consist oftype
andid
). It uses thetype
element from JSON:API to list the available derived schemas in its discriminator mapping.personDataInResponse
,tagDataInResponse
andtodoItemDataInResponse
useallOf
to refer to the base schema, followed by defining their additional properties.personCollectionResponseDocument
(I've left out the others for brevity) has anincluded
property, which is an array of typedataInResponse
.When using NSwag, the discriminator is used to generate derived classes, so you can write type checks on the included resources. For example:
As shown in the picture below, the compiler (Resharper in this case) is now aware of the base and derived classes:
Getting this feature to work was quite a journey! I started with implementing
oneOf
as documented by Swashbuckle, but it turned out that NSwag doesn't support that. I then tried switching toallOf
, which didn't work either. I couldn't figure out how to make NSwag generate the derived types properly; it would only generate the first derived type. So I built an API using NSwag to see what it needed to look like. Turned out that Swashbuckle usingallOf
is fundamentally broken. Because the project hasn't been updated in a year (pretty concerning), I didn't bother creating a PR with my fix. So instead I included a patched copy ofSchemaGenerator
in our codebase.When I tried mapping the JSON:API
type
field as the discriminator property, I found that NSwag would not expose theType
property anymore. This is great because that's just a JSON:API protocol detail that consumers should not need to worry about. So I thought it would be nice to have the same for the returned array from relationship endpoints. It didn't work out, because a bug in NSwag (actually, NSchema) resulted in generating the derived types asabstract
because they didn't have any properties of their own. To work around that, I would need to turn offx-abstract
in the base schema we produce.I'm also experimenting with Kiota, a fairly new OpenAPI client generator built by Microsoft. It functions (after working around microsoft/kiota#3800), but because it always generates all properties as nullable (and does not hide the
Type
property like NSwag does, and does not populate it when sending a derived type), this made things worse: instead of passing an enum, it would now require the consumer to pass a string like "todoItems" for the resource type. The same problem would occur when trying to use inheritance for the POST/PATCH resource request body, so I didn't even try. I also found that Kiota generates multiple identical derived classes (I haven't investigated further yet), so I didn't like the idea of making it more complicated than needed. Therefore I reverted all that. And that's why this PR only implements inheritance for theincluded
array.Closes #1059, fixes #1233.
QUALITY CHECKLIST