|
4 | 4 | using Microsoft.AspNetCore.Mvc; |
5 | 5 | using Microsoft.AspNetCore.Mvc.Formatters; |
6 | 6 | using Microsoft.Extensions.DependencyInjection; |
7 | | -using Microsoft.OpenApi.Models; |
8 | 7 |
|
9 | 8 | namespace JsonApiToolkit.Extensions |
10 | 9 | { |
@@ -64,73 +63,6 @@ public static IServiceCollection AddJsonApiToolkit(this IServiceCollection servi |
64 | 63 | services.AddScoped<JsonApiExceptionFilter>(); |
65 | 64 | services.AddScoped<JsonApiContentTypeFilter>(); |
66 | 65 |
|
67 | | - // Register OpenAPI document transformer for JSON:API content types |
68 | | - services.AddOpenApi(o => |
69 | | - { |
70 | | - // OpenAPI document transformer that rewrites request and response content types |
71 | | - // to "application/vnd.api+json" for all endpoints tagged as "JsonApi". |
72 | | - o.AddDocumentTransformer( |
73 | | - (document, _, _) => |
74 | | - { |
75 | | - foreach (var path in document.Paths.Values) |
76 | | - { |
77 | | - foreach (var operation in path.Operations.Values) |
78 | | - { |
79 | | - // Only apply to operations tagged as "JsonApi" |
80 | | - if (operation.Tags.Any(t => t.Name == "JsonApi")) |
81 | | - { |
82 | | - // Rewrite request body content types |
83 | | - if (operation.RequestBody != null) |
84 | | - { |
85 | | - if ( |
86 | | - operation.RequestBody.Content.TryGetValue( |
87 | | - "application/json", |
88 | | - out var jsonContent |
89 | | - ) |
90 | | - ) |
91 | | - { |
92 | | - operation.RequestBody.Content[ |
93 | | - "application/vnd.api+json" |
94 | | - ] = jsonContent; |
95 | | - operation.RequestBody.Content.Remove( |
96 | | - "application/json" |
97 | | - ); |
98 | | - } |
99 | | - else if ( |
100 | | - !operation.RequestBody.Content.ContainsKey( |
101 | | - "application/vnd.api+json" |
102 | | - ) |
103 | | - ) |
104 | | - { |
105 | | - operation.RequestBody.Content[ |
106 | | - "application/vnd.api+json" |
107 | | - ] = new OpenApiMediaType(); |
108 | | - } |
109 | | - } |
110 | | - |
111 | | - // Rewrite response content types |
112 | | - foreach (var response in operation.Responses.Values) |
113 | | - { |
114 | | - if ( |
115 | | - response.Content.TryGetValue( |
116 | | - "application/json", |
117 | | - out var jsonContent |
118 | | - ) |
119 | | - ) |
120 | | - { |
121 | | - response.Content["application/vnd.api+json"] = |
122 | | - jsonContent; |
123 | | - response.Content.Remove("application/json"); |
124 | | - } |
125 | | - } |
126 | | - } |
127 | | - } |
128 | | - } |
129 | | - return Task.CompletedTask; |
130 | | - } |
131 | | - ); |
132 | | - }); |
133 | | - |
134 | 66 | return services; |
135 | 67 | } |
136 | 68 | } |
|
0 commit comments