11using System ;
22using System . Collections . Generic ;
3+ using System . Linq ;
34using Microsoft . OpenApi ;
45using OpenAPI . WebApiGenerator . Extensions ;
56
@@ -23,7 +24,10 @@ public RequestBodyGenerator(
2324 List < RequestBodyContentGenerator > contentGenerators )
2425 {
2526 _body = body ;
26- _contentGenerators = contentGenerators ;
27+ _contentGenerators = contentGenerators
28+ . OrderByDescending ( generator =>
29+ generator . ContentType . GetPrecedence ( ) )
30+ . ToList ( ) ;
2731 }
2832
2933 internal static readonly RequestBodyGenerator Empty = new ( ) ;
@@ -71,7 +75,7 @@ public string GenerateRequestProperty(string propertyName)
7175/// <summary>
7276/// Request content
7377/// </summary>
74- internal sealed class RequestContent
78+ internal sealed class RequestContent(string? requestContentType, bool invalidContentType = false)
7579{{{
7680 _contentGenerators.AggregateToString(content =>
7781 content.GenerateRequestProperty()).Indent(4)}}
@@ -88,21 +92,21 @@ internal sealed class RequestContent
8892 var requestContentType = request.ContentType;
8993 var requestContentMediaType = requestContentType == null ? null : System.Net.Http.Headers.MediaTypeHeaderValue.Parse(requestContentType);
9094
91- switch (requestContentMediaType?.MediaType?.ToLower() )
95+ switch (requestContentMediaType?.MediaType)
9296 {{{_contentGenerators.AggregateToString(content =>
9397$$"""
94- case " {{content.ContentType.ToLower( )}}" :
95- return new RequestContent
98+ case not null when { { content. ContentType . GetMatchConditionExpression ( "requestContentMediaType" ) } } :
99+ return new RequestContent ( requestContentType )
96100 {
97101{ { content . GenerateRequestBindingDirective ( ) . Indent ( 20 ) } }
98102 } ;
99103""")}}{{(_body.Required ? "" :
100104"""
101- case "" :
105+ case null :
102106 return null ;
103107""")}}
104108 default:
105- throw new BadHttpRequestException($"Request body does not support content type { requestContentType}" );
109+ return new RequestContent( requestContentType, true );
106110 }
107111 }
108112
@@ -112,22 +116,19 @@ internal sealed class RequestContent
112116 /// <param name="validationContext">Current validation context</param>
113117 /// <param name="validationLevel">Validation level</param>
114118 /// <returns>The validation result</returns>
115- internal ValidationContext Validate(ValidationContext validationContext, ValidationLevel validationLevel)
116- {
117- switch (true)
119+ internal ValidationContext Validate(ValidationContext validationContext, ValidationLevel validationLevel) =>
120+ true switch
118121 {{{_contentGenerators.AggregateToString(content =>
119122$"""
120- case true when { content. PropertyName } is not null :
121- return { content . PropertyName } ! . Value . Validate ( "{content.SchemaLocation}" , true , validationContext , validationLevel ) ;
122- """)}}
123- default:
124- {{(_body.Required ?
125- """
126- throw new InvalidOperationException( "Request body not set" ) ;
127- """ :
128- "return validationContext;")}}
129- }
130- }
123+ true when { content. PropertyName } is not null =>
124+ { content . PropertyName } ! . Value . Validate ( "{content.SchemaLocation}" , true , validationContext , validationLevel ) ,
125+ """)}}
126+ true when requestContentType is null =>
127+ {{(_body.Required ? """ validationContext . WithResult ( false , "Request content is missing" ) """ : "validationContext")}},
128+ true when invalidContentType =>
129+ validationContext.WithResult(false, $"Request content type {requestContentType} is not supported"),
130+ _ => validationContext
131+ };
131132}
132133""" ;
133134 }
0 commit comments