Repository navigation
Open
Milestone
Description
The AddComponent API currently returns a boolean that indicates if the addition of the component to the document registry succeeded.
It would be nice if the API returned a reference to the item that was just constructed. In an M.A.OpenAPI transformer, I currently need to write:
options.AddOperationTransformer((operation, context, cancellationToken) =>
{
var schemaService = context.ApplicationServices.GetRequiredKeyedService<IOpenApiSchemaService>(context.DocumentName);
if (context.Description.RelativePath == "error")
{
var errorSchema = schemaService.GetOrCreateSchema(typeof(ProblemDetails));
context.Document.AddComponent("Error", errorSchema);
operation.Responses["500"] = new OpenApiResponse
{
Description = "Error",
Content =
{
["application/problem+json"] = new OpenApiMediaType
{
Schema = new OpenApiSchemaReference("Error", context.Document),
},
},
};
}
return Task.CompletedTask;
});but would like to write:
options.AddOperationTransformer((operation, context, cancellationToken) =>
{
var schemaService = context.ApplicationServices.GetRequiredKeyedService<IOpenApiSchemaService>(context.DocumentName);
if (context.Description.RelativePath == "error")
{
var errorSchema = schemaService.GetOrCreateSchema(typeof(ProblemDetails));
var insertedSchema = context.Document.AddComponent("Error", errorSchema);
operation.Responses["500"] = new OpenApiResponse
{
Description = "Error",
Content =
{
["application/problem+json"] = new OpenApiMediaType
{
Schema = insertedSchema,
},
},
};
}
return Task.CompletedTask;
});cc: Vincent Biret (@baywet) I couldn't see if an API that did this existed already and figured I'd add the proposal.
Metadata
Metadata
Assignees
Labels
No labels
Activity
baywet commentedon Feb 24, 2025
Hi Safia Abdalla (@captainsafia)
We do have constructors overloads that are internal at the moment and would allow you to pass the source schema directly.
Effectively allowing you to write something like this
I'm not saying this would be better than your proposal, just stating it's a possibility.
I think it'd be preferable to separate the two concerns, and offer something like that instead.
(which would return a value or throw, we could also have a TryGet variant)
what do you think?
captainsafia commentedon Feb 24, 2025
Why do you think its preferable to separate?
IMO, having the
AddComponentAPI makes it a little easier to connect the fact that the object that is inserted into a Schema property is a reference to the thing that was added to components. The two concepts seem linked in that way to me.baywet commentedon Feb 25, 2025
Because: