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

Umbrella: OpenAPI integration #1046

Open
maurei opened this issue Sep 1, 2021 · 14 comments
Open

Umbrella: OpenAPI integration #1046

maurei opened this issue Sep 1, 2021 · 14 comments
Labels

Comments

@maurei
Copy link
Member

maurei commented Sep 1, 2021

OpenAPI integration

This document provides an overview of the work items involved in creating an OpenAPI integration for JsonApiDotNetCore.

Disclaimer: this is an open source project. The available time of our contributors varies and therefore we do not plan release dates. This document expresses our current intent, which may change over time.

Approach

Creating an OpenAPI integration for JADNC requires setting up a mechanism that generates an OpenAPI Specification (OAS) file from application code. This file allows for automatic generation of API docs (see eg. SwaggerUI) and client code (see eg. NSwag code generator). In the design choices for this mechanism precedence will be given to compatibility with client code generation tools because we believe this to be of the biggest impact to the JSON:API community (see eg. this issue and others).

In a private repository we have experimented for several months with integrating Swashbuckles SwaggerGen in a JADNC application. We have decided to extract this code into this repository and move forward from there.

Work items

The order of the list below reflects the order in which we intend to address the work items.

The state of the integration can be viewed in the openapi branch.

Feedback

The best way to give feedback is to create new issues or upvote/downvote existing ones.
Please give us feedback that will give us insight on the following points:

  • Existing features that are missing some capability or otherwise don't work well enough.
  • Missing features that should be added to the product.
  • Design choices for a feature that is currently in-progress.
@wunki
Copy link

wunki commented Sep 1, 2021

This is so exciting! This will catapult JADNC to the next level 🚀

@bkoelman
Copy link
Member

bkoelman commented Dec 8, 2022

For anyone that wants to try out the latest bits: pick a version with "openapi" in the name from https://github.com/json-api-dotnet/JsonApiDotNetCore#trying-out-the-latest-build.

@sharky98

This comment was marked as off-topic.

@bkoelman

This comment was marked as off-topic.

@bkoelman
Copy link
Member

bkoelman commented Oct 20, 2023

JetBrains Rider has built-in IDE support for OpenAPI, see https://www.jetbrains.com/help/rider/OpenAPI.html.
Similar feature in Visual Studio Preview: https://devblogs.microsoft.com/visualstudio/web-api-development-in-visual-studio-2022/#endpoints-explorer

@verdie-g
Copy link
Contributor

verdie-g commented Feb 7, 2024

@bkoelman what's the status of the OpenAPI integration? I could have a look if there is a isolated issue about it.

@bkoelman
Copy link
Member

bkoelman commented Feb 7, 2024

@verdie-g The OpenAPI integration is my primary priority. Picking up from @maurei's work, I've recently implemented support for triple-slash doc-comments, query string parameters, and included related resources in responses.

I've updated the list of work items and marked several with the good first issue label. Feel free to give it a try.

@maurei
Copy link
Member Author

maurei commented Feb 21, 2024

Happy to see this work is being picked up again! I wish I could devote a few months of my time to this again :-)

@bkoelman
Copy link
Member

OpenAPI generation is being added in ASP.NET 9, replacing the Swashbuckle support in Visual Studio.

@verdie-g
Copy link
Contributor

OpenAPI generation is being added in ASP.NET 9

Does it change the plans regarding this issue? If the current openapi branch is merged into master I suppose it will be hard to migrate to Microsoft.AspNetCore.OpenApi so maybe we should consider doing the migration now. Note that Microsoft.AspNetCore.OpenApi targets .NET 7+ and JADNC targets .NET 6+. Though, .NET 6 EOL is in November of this year so I suppose JADNC could drop .NET 6 then.

@bkoelman
Copy link
Member

OpenAPI generation is being added in ASP.NET 9

Does it change the plans regarding this issue? If the current openapi branch is merged into master I suppose it will be hard to migrate to Microsoft.AspNetCore.OpenApi so maybe we should consider doing the migration now. Note that Microsoft.AspNetCore.OpenApi targets .NET 7+ and JADNC targets .NET 6+. Though, .NET 6 EOL is in November of this year so I suppose JADNC could drop .NET 6 then.

I've tried the built-in support in ASP.NET 9 preview4. It doesn't work at all with what we have today. The first issue is that the ApiExplorer integration (rewriting endpoints) is handled differently. The ASP.NET implementation is still in a very early stage and under active development. I've expressed our needs at dotnet/aspnetcore#54598 (comment), but the extension points we need don't yet exist. I don't think it makes sense to spend a lot of time on adopting it in the near future. And because Swashbuckle is now under active maintenance again, I suspect to finish our OpenAPI integration based on that. Because it is mature and stable, and our extensibility needs go way beyond what most other projects need.

@captainsafia
Copy link

@bkoelman Following up on the comment that you left in the issue on the ASP.NET Core repo here.

The Microsoft.AspNetCore.OpenApi implementation has evolved to cover some of the scenarios that you listed originally including:

  • Creating custom reference ideas via OpenApiOptions.CreateSchemaReferenceId (in PR)
  • Creating custom operation IDs for types (supported via EndpointName metadata)
  • Reording paths in a document (supported via DocumentTransformers)
  • Using different schemas to represent requests/responses by differing required/nullable states (supported by M.A.OpenAPI's schema-comparison based implementation for generating refs)

The biggest gap I noticed is around your schema generation-based customizations. Our implementation currently relies on the new JsonSchemaExporter APIs in System.Text.Json (.NET 9 Preview 6) and that layer doesn't have as many of the customizability options that Swashbuckle has. That being said, I'd be curious to understand the details of some of the items you mentioned in your bullet list from the comment above:

Custom handling of OpenAPI inheritance with allOf and discriminator that maps to JSON:API type

Do you use allOf to model inheritance hierarchies in your documents or is that something that you disable? This isn't an option that we currently support in M.A.OpenApi, primarily because its iffy whether or not allOf can be used to accuratnely model inheritance hierarchies in JSON schema.

What discriminators are you referring to in the statement above? At the moment, our implementation supports STJ's polymorphism attributes by default. Does your library provide a custom set?

For the JSON:API type, we generate a single-valued enum, so callers don't need to set a magic string

I assume you're doing this to work around the limitation in OpenAPI v3 that the JSON Schema const keyword is not supported? If so, I don't really have a good solution for that since our system has to comply with a similar constraint. We do expose a schema transformers concept (similar to schema filters) that can help here.

In JSON:API, sending null means to clear something, whereas not sending it means to leave it unchanged. So we need to handle OpenAPI required/nullable manually, ie [Required] on a property must not translate to non-nullable

Requiredness/nullability are modeled independently in our system so you should be able to model required properties that can be null if need be.

But to a more general point, I think your statement here:

And because Swashbuckle is now under active maintenance again, I suspect to finish our OpenAPI integration based on that. Because it is mature and stable, and our extensibility needs go way beyond what most other projects need.

is prudent and wise. It seems like you've done a lot of work in the area already building our Swashbuckle's model and I wouldn't through that away. In the future, it would be interesting to explore if you can extend this to support M.A.OpenAPI as well, and perhaps go even further and recommend some API changes to the underlying ApiExplorer metadata layer that both Swashbuckle/M.A.OpenAPI use for document generation to make your framework more resilient to whatever API document spec is supported.

I hope this information was helpful! If you have any inquiries about this work (particularly in regard to the more ASP.NET Core-related aspects), I'm happy to share insights.

@bkoelman
Copy link
Member

Hi @captainsafia,

Thanks for the follow-up, I really appreciate the proactivity here.
I've created #1591 to respond and explore this further.

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

No branches or pull requests

6 participants