Skip to content

Commit 1add738

Browse files
authored
Don't produce request body on GET (CarterCommunity#188)
1 parent 26ddd0c commit 1add738

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

samples/CarterSample/Features/Actors/OpenApi/SampleMetaData.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
namespace CarterSample.Features.Actors
22
{
3+
using System;
34
using Carter.OpenApi;
45

56
public class SampleMetaData : RouteMetaData
67
{
8+
public override Type Request { get; } = typeof(Foo);
9+
710
public override RouteMetaDataResponse[] Responses { get; } =
811
{
912
new RouteMetaDataResponse { Code = 200, Response = typeof(PagedItems<Foo>) }

src/OpenApi/CarterOpenApi.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@ private static void CreateOpenApiResponseBody(OpenApiDocument document, KeyValue
286286

287287
private static void CreateOpenApiRequestBody(OpenApiDocument document, KeyValuePair<(string verb, string path), RouteMetaData> keyValuePair, OpenApiOperation operation, HttpContext context)
288288
{
289+
if (keyValuePair.Key.verb == "GET")
290+
{
291+
return;
292+
}
293+
289294
if (keyValuePair.Value.Request != null)
290295
{
291296
bool arrayType = false;
@@ -317,22 +322,19 @@ private static void CreateOpenApiRequestBody(OpenApiDocument document, KeyValueP
317322
propObj.Add(propertyInfo.Name, new OpenApiString("")); //TODO Could use Bogus to generate some data rather than empty string
318323
}
319324

320-
321-
322325
var schema = new OpenApiSchema
323326
{
324327
Type = "object",
325328
Properties = propNames.ToDictionary(key => key.Name, value => new OpenApiSchema { Type = GetOpenApiTypeMapping(value.Type) }),
326329
Example = propObj
327330
};
328-
331+
329332
var validatorLocator = context.RequestServices.GetRequiredService<IValidatorLocator>();
330333

331334
var validator = validatorLocator.GetValidator(requestType);
332335

333336
if (validator != null)
334337
{
335-
336338
var validatorDescriptor = validator.CreateDescriptor();
337339

338340
//Thanks for the pointers https://github.com/micro-elements/MicroElements.Swashbuckle.FluentValidation

0 commit comments

Comments
 (0)