-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Cannot have multiple response types for same status code #2743
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
Labels
help-wanted
A change up for grabs for contributions from the community
Comments
This would definitely be a very nice feature to add... even if just the support for multiple schemas for same status code. |
Closed
1 task
Until this is implemented, I just wrote a basic operation filter that accomplishes this. Feel free to use it. public class MultipleProducesOperationFilter : IOperationFilter {
public void Apply(OpenApiOperation operation, OperationFilterContext context) {
var attrs = context.MethodInfo.GetCustomAttributes(false)
.OfType<ProducesResponseTypeAttribute>()
.GroupBy(x => x.StatusCode)
.Select(g => new { StatusCode = g.Key, Attributes = g.ToArray() })
.ToList();
var duplicateAttrs = attrs
.Where(x => x.Attributes.Length > 1)
.ToList();
if (duplicateAttrs.Count == 0) {
return;
}
// We have multiple attributes. Rebuild the responses.
foreach (var details in duplicateAttrs) {
var schema = BuildSchemaFromAttrs(details.Attributes, context);
// I don't think there's a scenario where we would have an attribute on the method, but not
// also have a response for the corresponding code in the dict. If that does happen, this will throw.
var response = operation.Responses[details.StatusCode.ToString()];
foreach (var contentType in response.Content.Keys) {
var contentDetails = response.Content[contentType];
contentDetails.Schema = schema;
}
}
}
private static OpenApiSchema BuildSchemaFromAttrs(IEnumerable<ProducesResponseTypeAttribute> attrs, OperationFilterContext context) {
var schemas = new List<OpenApiSchema>();
foreach (var attr in attrs) {
// Expect that these will be generated as references, not inline
var schema = context.SchemaGenerator.GenerateSchema(attr.Type, context.SchemaRepository);
schemas.Add(schema);
}
return new() {
OneOf = schemas,
};
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I am doubtful that if this existing issue (#2413, #329) will be reopened, so here I open a new one.
OneOf is now supported by open-api standard. I believe this is a basic feature that would be useful for many.
I wonder how much trouble, the swagger docs now available in internet generated by .NET applications would be creating for consumers of those APIs. 🙂
https://spec.openapis.org/oas/v3.1.0#discriminator-object
The text was updated successfully, but these errors were encountered: