aspnetcore: IOpenApiDocumentTransformer cannot modify components.Schemas since it's null
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
When running IOpenApiDocumentTransformer, it would be logical to have the ability to modify the OpenApiDocument.Components.Schemas to make any adjustments. The OpenApiDocument.Components is null.
The await _schemaReferenceTransformer.TransformAsync(document, documentTransformerContext, cancellationToken) populates the Components.Schemas, but that happens after the document transformers are run.
Expected Behavior
It should be possible to modify Components.Schemas in the IOpenApiDocumentTransformer
Steps To Reproduce
No response
Exceptions (if any)
No response
.NET Version
.NET 9 RC2
Anything else?
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 2
- Comments: 15 (5 by maintainers)
How does this new api allow us to enumerate all the existing schemas in the document? Some things can be done in a schema/operation transformer, but some things are easier to do when you have access to the totality. This might be an edge case, but when you have something called an
IOpenApiDocumentTransformeryou kinda expect to have access to the whole document.It’s a huge oversight that the
IOpenApiDocumentTransformerisn’t the last stop in document generation. What use is it if we don’t have access to the complete specification?I have problems with this too. I want to remove some specific schemas that are generated by default. I can’t use a schematransformer to remove a schema, and i can’t edit the nulled list of schemas in the documenttransformer.
I think I commented somewhere else, but I came up with this way after understanding that the annotation
"x-schema-idis controls the naming:Polymorphic classes:
Yes, changing name doesn’t work for polymorphic classes since that is dealt by its own private set of transformer methods that are run by default after the transformers have run.
Would be nice if those could become regular transformers, and that there would be transformers that allowed for inheritance, similar to what NSwag and Swashbuckle does by default.
On the
IOpenApiDocumentTransformer:Transformers should be seen as middleware, if that makes sense. But the final composition stage happens after all the transformer have run. That is when similar schemas are being merged and placed in Schemas in OpenApiDocument.
Because of what is mention above, it makes sense that at the transformer stage the “Components.Schemas” are not populated. But still confusing that the property is there if you, as a developer, don’t understand this. Because you might expect that a
IOpenApiDocumentTransformerhas the complete document, which it does not have.Question:
Should there be a way to hook into when a document has been fully composed? That would give you ability to do finishing touches. Even if risky.
You can use this middleware to modify the response on the way out. In this example I’m removing well-known types that I don’t want in the response. They key part is loading it into the
OpenApiStringReaderwhich then allows you to modify any part of the document;To clarify, the code sample only works for .NET 10 because we take a dependency on some API changes in Microsoft.OpenApi v2. This isn’t feasible in .NET 9 and we likely won’t be making any retroactive changes to support it.
In the new model, you do have access to the all the schemas in the document since they are inserted into the document as they are constructed instead of via a document transformer as the final step (as in the current model).
The following code:
will write the following to the console:
Unfortunately that code sample isn’t working for me - document.Components is always null in the document transformer.
In my case I have an OData endpoint which causes the Open API document to include a bunch of objects not really relevant to the API end users:
Using Swashbuckle.AspNetCore.SwaggerGen I am able to remove schemas from the final open API document using the IDocumentFilter interface and the document.Components.Schemas.Remove() method in code similar to this:
I am not seeing any way to remove schemas in the new Microsoft.AspNetCore.OpenApi IOpenApiDocumentTransformer since the Components object is always null on the document:
Is there any way to remove or update schemas similar to how we currently can using the SwaggerGen package?
I’m finding it quite frustrating trying to extend the default spec for this same reason mentioned here. I want to have an API endpoint that allows for partial model fields to be provided, which would be quite easy in other languages/frameworks. I’ve also found that the generated doc does not respect the configuration specified by
WithOpenApion aRouteHandlerBuilder. I feel like anyIOpenApiDocumentTransformerimplementations we provide should be the last stop after all other generation has occurred so that we can customize to our exact needs.@dnv-kimbell This is expected behavior. If you are trying to make schema-level customizations, the
IOpenApiSchemaTransformerAPI is the one that you want to use.What are you trying to do?