Skip to content

How can I access OpenApiDocument at runtime #60330

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

Closed
EliveltonRepolho opened this issue Feb 12, 2025 · 8 comments · Fixed by #61463
Closed

How can I access OpenApiDocument at runtime #60330

EliveltonRepolho opened this issue Feb 12, 2025 · 8 comments · Fixed by #61463
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.
Milestone

Comments

@EliveltonRepolho
Copy link

How can I access OpenApiDocument with new OpenApi package ?

I can see that today the only options are during build time and using the mapped endpoint, but will only have the jsom/yaml file. I wanted to access the OpenApiDocument at runtime so I can work with some metadata I need to expose based on the OpenApiDocument.

Here where I found the endpoint mapped that access OpenApiDocumentService to generate the OpenApi document, but they are all internal classes.

endpoints.MapGet(pattern, async (HttpContext context, string documentName = OpenApiConstants.DefaultDocumentName) =>
{
  ...
  var documentService = context.RequestServices.GetKeyedService<OpenApiDocumentService>(lowercasedDocumentName);
  .... 
  var document = await documentService.GetOpenApiDocumentAsync(context.RequestServices, context.RequestAborted);
  ....
}

Thanks

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Feb 12, 2025
@martincostello
Copy link
Member

By implementing a custom document transformer, you have access to the document to change it as you see fit before it is returned to the client: https://learn.microsoft.com/aspnet/core/fundamentals/openapi/customize-openapi

@EliveltonRepolho
Copy link
Author

By implementing a custom document transformer, you have access to the document to change it as you see fit before it is returned to the client: https://learn.microsoft.com/aspnet/core/fundamentals/openapi/customize-openapi

yeah, I did implement my customizations, the OpenApi document which is returned by the endpoint looks good, but how can I access the OpenApiDocument with all the transformations applied, same document that is returned in the endpoint.

@martincostello
Copy link
Member

Ah I see - I don't think there's a way to do that.

@captainsafia
Copy link
Member

@EliveltonRepolho Thanks for filing this issue!

@martincostello's response seems correct based on my understanding of the question. Currently, there's no public API for accessing the in-memory OpenAPI document.

Out of curiousity, how and where are you trying to access the OpenApiDocument?

@captainsafia captainsafia added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Feb 13, 2025
@captainsafia captainsafia added this to the Backlog milestone Feb 13, 2025
@EliveltonRepolho
Copy link
Author

@EliveltonRepolho Thanks for filing this issue!

@martincostello's response seems correct based on my understanding of the question. Currently, there's no public API for accessing the in-memory OpenAPI document.

Out of curiousity, how and where are you trying to access the OpenApiDocument?

Got it, our use case is for a internal framework build for a Backend For Front (BFF), which exposes metadata related to the endpoints (similar to OpenAPI json but with different schema, and with some enrichment), since it is generated on backend side, we take advantage of all properties already present in OpenApiDocument, with extra enrichment processes for internal attributes.

@dotnet-policy-service dotnet-policy-service bot added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Feb 13, 2025
@captainsafia
Copy link
Member

@EliveltonRepolho In the example above, is your endpoint returning the OpenAPI document back in the response or do you return your own type that you derive from the OpenAPI document?

What metadata from the document do you pull?

I might have a proposed resolution for you based on those questions. 🤔

@EliveltonRepolho
Copy link
Author

@captainsafia in the example, we return our own type which is derived from OpenAPI document.

What we do is:

  • We use document transformer to include our custom metada to OpenAPI, which is exposed to openapi endpoint/document
  • Then we have our own endpoint, which uses OpenAPI Document to create our custom response type based on custom metada included as well default OpenAPI meta (required, length, permissions/roles, query params, body ...)
  • The reason we don't use the OpenAPI document directly is because our custom endpoint first "curate" the metadata and create the response especifically for that User/App, so it consider feature flags and permissions for that particupar request

@captainsafia
Copy link
Member

@EliveltonRepolho Ah, I see. I was going to recommend using the underlying IApiDescriptionProvider APIs to resolve the metadata that you need instead of the OpenAPI dcument. The IApiDescriptionProvider interface is the same one that OpenAPI implementations pull from to construct the document. You might find that it is higher fidelity than what is available in the OpenAPI document.

Absent this, you might have to leverage private reflection (for now) to resolve the OpenAPI document service and interact with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants