-
Notifications
You must be signed in to change notification settings - Fork 10k
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
.NET9: Microsoft.AspNetCore.OpenApi.OpenApiSchemaService should not be internal #57798
Comments
I assume you mean "not included in the ASP.NET Core 9 templates"? Swashbuckle.AspNetCore itself is still supported by its maintainers (of which I'm one), and there's support for .NET 9 in progress: domaindrivendev/Swashbuckle.AspNetCore#3007
You should be able to add additional schema components using |
That's exactly what I'm trying to do! The problem is how do we create the schema for a type? One example: The API is set up to handle and return exceptions as a custom type ErrorResponse. Migrating this filter to an IOpenApiOperationTransformer implementation, we have this code: public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken)
{
if (operation.Responses.ContainsKey("500"))
{
return Task.CompletedTask;
}
// Next line is old Swashbuckle code - How can we do convert the type ErrorResponse to OpenApiSchema using IOpenApiOperationTransformer?
// var schema = context.SchemaGenerator.GenerateSchema(typeof(ErrorResponse), context.SchemaRepository);
var serverError = new OpenApiResponse
{
Description = "Server Error",
Content =
{
{ "application/json", new OpenApiMediaType { Schema = schema } }
}
};
operation.Responses.Add("500", serverError);
return Task.CompletedTask;
} So the problem is, when we can't access the OpenApiSchemaService.GetOrCreateSchemaAsync, how are we supposed to get the schema for the custom type ErrorRespose? |
My understanding is that, given a type, you can use the new var schemaObject = JsonSchemaExporter.GetJsonSchemaAsNode(jsonSerializerOptions, typeof(ErrorResponse), jsonSchemaExporterOptions);
// Create an OpenApiSchema from the JsonNode returned above That's not something I've done myself though. Rather than get into adding a schema directly, I've instead added metadata to the API endpoints to add additional produces metadata (e.g. |
Ok, so we basically need to copy most of the service (the jsonSchemaExporterOptions is huge, and we still need to deserialize JsonNode). It would have been nice to have this in a public method like Swashbuckle has. |
@jornhd Thanks for filing this issue and trying out the new APIs. The choice to make the I do have one curious question about your scenario though. Where is the I'd +1 @martincostello's recommendation above: if you can you should use |
We do this using middleware. I've learned now that we can add the response type to all methods by adding a custom convention with @martincostello @captainsafia Thanks for your inputs. You can close this issue. |
This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes. See our Issue Management Policies for more information. |
Background and Motivation
As Swashbuckle is no longer supported in .NET 9, we've started migrating our operation filters to operation transformers to prepare for the .NET 9 release.
In some of our current Swashbuckle filters, we need to add types to the schema that isn't described in the ApiDescription, in order to reference these types in eg. operation.Responses.
We do this today with the following line:
var schema = context.SchemaGenerator.GenerateSchema(myType, context.SchemaRepository);
We tried to do the same in our IOpenApiOperationTransformer implementation:
The problem is that OpenApiSchemaService is internal, not public, so it's impossible to add schema for additional types.
Proposed API
Make OpenApiSchemaService public, or provide GetOrCreateSchemaAsync in a public api, preferable the OpenApiOperationTransformerContext.
The text was updated successfully, but these errors were encountered: