-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add API to support dyanmically generated OpenAPI schemas in document #60589
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
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
Moving this to Preview 4. The proposal still hasn't gone through API review and likely won't before code complete next week. |
API Review Notes:
API Approved! namespace Microsoft.AspNetCore.OpenApi;
public sealed class OpenApiDocumentTransformerContext
{
+ public Task<OpenApiSchema> GetOrCreateSchemaAsync(Type type, ApiParameterDescription? parameterDescription = null, CancellationToken cancellationToken = default);
}
public sealed class OpenApiOperationTransformerContext
{
+ public OpenApiDocument? Document { get; init; }
+ public Task<OpenApiSchema> GetOrCreateSchemaAsync(Type type, ApiParameterDescription? parameterDescription = null, CancellationToken cancellationToken = default);
}
public sealed class OpenApiSchemaTransformerContext
{
+ public OpenApiDocument? Document { get; init; }
+ public Task<OpenApiSchema> GetOrCreateSchemaAsync(Type type, ApiParameterDescription? parameterDescription = null, CancellationToken cancellationToken = default);
} |
Closing as supported in preview4. |
Background and Motivation
The current OpenAPI implementation in ASP.NET Core lacks support for being able to dynamically generate OpenAPI schemas and insert them into the document. Without built-in APIs for this, evelopers must manually write the schemas for types in their application that need to be referenced automatically. While this can be straightforward for simple types, it grows complicated for DTOs and more complex types in the user application. Furthermore, developers can't take advantage of the built-in logic the OpenAPI implementation has to support resolving metadata from
ParameterInfo
and route constraints.GetOrCreateSchema
API takes a type and an ApiParameterDescription to support being able to generate schemas for the type and apply any metadata from theParameterInfo
to it.Document
property is added to the transformer contexts to allow users to interact with the components store, provided by the underlyingMicrosoft.OpenApi
library.Proposed API
Usage Examples
Alternative Designs
N/A
Design Notes
The
IOpenApiSchemaProvider
type is not applied on theOpenApiSchemaService
registered in the DI container. We opted not to do this in favor of restricting schema generation to the transformer APIs. This avoids theMicrosoft.AspNetCore.OpenApi
package becoming a generic schema generation API and allows us to limit the functionality to codepaths that we have more control over (OpenAPI document transformers).Schema transformers registered in an API are applied to schemas that are generated by the service. This can increase the risk for re-entrant or recursive schema transformers to generate too many schemas if developers do not add guards against these cases.
Risks
The text was updated successfully, but these errors were encountered: