private void SetResponseExamples(OpenApiOperation operation, OperationFilterContext context) { var responseAttributes = context.GetControllerAndActionAttributes <SwaggerResponseExampleAttribute>(); foreach (var attr in responseAttributes) { var example = serviceProvider.GetExampleWithExamplesProviderType(attr.ExamplesProviderType); responseExample.SetResponseExampleForStatusCode( operation, attr.StatusCode, example); } }
private void SetResponseModelExamples(Operation operation, OperationFilterContext context) { var responseAttributes = context.GetControllerAndActionAttributes <SwaggerResponseExampleAttribute>(); foreach (var attr in responseAttributes) { var examplesProvider = (IExamplesProvider)(serviceProvider.GetService(attr.ExamplesProviderType) ?? Activator.CreateInstance(attr.ExamplesProviderType)); object example = examplesProvider?.GetExamples(); responseExample.SetResponseExampleForStatusCode(operation, attr.StatusCode, example, attr.ContractResolver, attr.JsonConverter); } }
private void SetResponseExamples(Operation operation, OperationFilterContext context) { var actionAttributes = context.MethodInfo.GetCustomAttributes <SwaggerResponseExampleAttribute>(); var responseAttributes = context.GetControllerAndActionAttributes <ProducesResponseTypeAttribute>(); foreach (var response in responseAttributes) { if (actionAttributes.Any(a => a.StatusCode == response.StatusCode)) { continue; // if [SwaggerResponseExample] is defined, then let ExamplesOperationFilter define the example } var example = GetExampleForTypeFromServiceProvider(response.Type); responseExample.SetResponseExampleForStatusCode(operation, response.StatusCode, example); } }
private void SetResponseExamples(OpenApiOperation operation, OperationFilterContext context) { var actionAttributes = context.GetControllerAndActionAttributes <SwaggerResponseExampleAttribute>().Select(a => new StatusCodeWithType(a.StatusCode, a.ExamplesProviderType)); var responseAttributes = context.GetControllerAndActionAttributes <ProducesResponseTypeAttribute>().Select(a => new StatusCodeWithType(a.StatusCode, a.Type)); var autodetectedResponses = context.ApiDescription.SupportedResponseTypes.Select(r => new StatusCodeWithType(r.StatusCode, r.Type)); var responses = responseAttributes.Concat(autodetectedResponses); foreach (var response in responses) { if (actionAttributes.Any(a => a.StatusCode == response.StatusCode)) { continue; // if [SwaggerResponseExample] is defined, then let ExamplesOperationFilter define the example } var example = serviceProvider.GetExampleForType(response.Type); responseExample.SetResponseExampleForStatusCode(operation, response.StatusCode, example); } }