3535import io .swagger .v3 .core .util .AnnotationsUtils ;
3636import io .swagger .v3 .oas .models .Components ;
3737import io .swagger .v3 .oas .models .media .Content ;
38+ import io .swagger .v3 .oas .models .media .Encoding ;
3839import io .swagger .v3 .oas .models .media .MediaType ;
3940import io .swagger .v3 .oas .models .media .Schema ;
4041import io .swagger .v3 .oas .models .parameters .RequestBody ;
42+ import jakarta .validation .constraints .NotNull ;
4143import org .apache .commons .lang3 .StringUtils ;
4244import org .springdoc .core .models .MethodAttributes ;
4345import org .springdoc .core .models .ParameterInfo ;
4648import org .springdoc .core .utils .SpringDocAnnotationsUtils ;
4749
4850import org .springframework .core .MethodParameter ;
51+ import org .springframework .util .CollectionUtils ;
4952import org .springframework .web .bind .annotation .RequestPart ;
5053
5154import static org .springdoc .core .utils .SpringDocAnnotationsUtils .mergeSchema ;
@@ -146,9 +149,9 @@ public Optional<RequestBody> buildRequestBodyFromDoc(
146149 * @param requestBodyObject the request body object
147150 */
148151 private void buildRequestBodyContent (io .swagger .v3 .oas .annotations .parameters .RequestBody requestBody ,
149- RequestBody requestBodyOp , MethodAttributes methodAttributes ,
150- Components components , JsonView jsonViewAnnotation , String [] classConsumes ,
151- String [] methodConsumes , RequestBody requestBodyObject ) {
152+ RequestBody requestBodyOp , MethodAttributes methodAttributes ,
153+ Components components , JsonView jsonViewAnnotation , String [] classConsumes ,
154+ String [] methodConsumes , RequestBody requestBodyObject ) {
152155 Optional <Content > optionalContent = SpringDocAnnotationsUtils
153156 .getContent (requestBody .content (), getConsumes (classConsumes ),
154157 getConsumes (methodConsumes ), null , components , jsonViewAnnotation , parameterBuilder .isOpenapi31 ());
@@ -296,12 +299,14 @@ private RequestBody buildRequestBody(RequestBody requestBody, Components compone
296299 if (requestBody .getContent () == null ) {
297300 Schema <?> schema = parameterBuilder .calculateSchema (components , parameterInfo , requestBodyInfo ,
298301 methodAttributes .getJsonViewAnnotationForRequestBody ());
299- buildContent (requestBody , methodAttributes , schema );
302+ Map <String , Encoding > parameterEncoding = getParameterEncoding (parameterInfo );
303+ buildContent (requestBody , methodAttributes , schema , parameterEncoding );
300304 }
301305 else if (!methodAttributes .isWithResponseBodySchemaDoc ()) {
302306 Schema <?> schema = parameterBuilder .calculateSchema (components , parameterInfo , requestBodyInfo ,
303307 methodAttributes .getJsonViewAnnotationForRequestBody ());
304- mergeContent (requestBody , methodAttributes , schema );
308+ Map <String , Encoding > parameterEncoding = getParameterEncoding (parameterInfo );
309+ mergeContent (requestBody , methodAttributes , schema , parameterEncoding );
305310 }
306311
307312 // Add requestBody javadoc
@@ -318,38 +323,40 @@ else if (!methodAttributes.isWithResponseBodySchemaDoc()) {
318323 /**
319324 * Merge content.
320325 *
321- * @param requestBody the request body
322- * @param methodAttributes the method attributes
323- * @param schema the schema
326+ * @param requestBody the request body
327+ * @param methodAttributes the method attributes
328+ * @param parameterEncoding the parameter encoding
324329 */
325- private void mergeContent (RequestBody requestBody , MethodAttributes methodAttributes , Schema <?> schema ) {
330+ private void mergeContent (RequestBody requestBody , MethodAttributes methodAttributes , Schema <?> schema , Map < String , Encoding > parameterEncoding ) {
326331 Content content = requestBody .getContent ();
327- buildContent (requestBody , methodAttributes , schema , content );
332+ buildContent (requestBody , methodAttributes , schema , content , parameterEncoding );
328333 }
329334
330335 /**
331336 * Build content.
332337 *
333- * @param requestBody the request body
334- * @param methodAttributes the method attributes
335- * @param schema the schema
338+ * @param requestBody the request body
339+ * @param methodAttributes the method attributes
340+ * @param schema the schema
341+ * @param parameterEncoding the parameter encoding
336342 */
337- private void buildContent (RequestBody requestBody , MethodAttributes methodAttributes , Schema <?> schema ) {
343+ private void buildContent (RequestBody requestBody , MethodAttributes methodAttributes , Schema <?> schema , Map < String , Encoding > parameterEncoding ) {
338344 Content content = new Content ();
339- buildContent (requestBody , methodAttributes , schema , content );
345+ buildContent (requestBody , methodAttributes , schema , content , parameterEncoding );
340346 }
341347
342348 /**
343349 * Build content.
344350 *
345- * @param requestBody the request body
346- * @param methodAttributes the method attributes
347- * @param schema the schema
348- * @param content the content
351+ * @param requestBody the request body
352+ * @param methodAttributes the method attributes
353+ * @param schema the schema
354+ * @param content the content
355+ * @param parameterEncoding the parameter encoding
349356 */
350- private void buildContent (RequestBody requestBody , MethodAttributes methodAttributes , Schema <?> schema , Content content ) {
357+ private void buildContent (RequestBody requestBody , MethodAttributes methodAttributes , Schema <?> schema , Content content , Map < String , Encoding > parameterEncoding ) {
351358 for (String value : methodAttributes .getMethodConsumes ()) {
352- io . swagger . v3 . oas . models . media . MediaType mediaTypeObject = new io . swagger . v3 . oas . models . media . MediaType ();
359+ MediaType mediaTypeObject = new MediaType ();
353360 mediaTypeObject .setSchema (schema );
354361 MediaType mediaType = content .get (value );
355362 if (mediaType != null ) {
@@ -360,8 +367,37 @@ private void buildContent(RequestBody requestBody, MethodAttributes methodAttrib
360367 if (mediaType .getEncoding () != null )
361368 mediaTypeObject .setEncoding (mediaType .getEncoding ());
362369 }
370+ else if (!CollectionUtils .isEmpty (parameterEncoding )) {
371+ mediaTypeObject .setEncoding (parameterEncoding );
372+ }
363373 content .addMediaType (value , mediaTypeObject );
364374 }
365375 requestBody .setContent (content );
366376 }
377+
378+ /**
379+ * Gets parameter encoding.
380+ *
381+ * @param parameterInfo the parameter info
382+ * @return the parameter encoding
383+ */
384+ @ NotNull
385+ private Map <String , Encoding > getParameterEncoding (ParameterInfo parameterInfo ) {
386+ if (parameterInfo .getParameterModel () != null ) {
387+ Content parameterContent = parameterInfo .getParameterModel ().getContent ();
388+ if (parameterContent != null && parameterContent .size () == 1 ) {
389+ Map <String , Encoding > encoding = parameterContent .values ().iterator ().next ().getEncoding ();
390+ if (!CollectionUtils .isEmpty (encoding )) {
391+ return encoding ;
392+ }
393+ else {
394+ String encodingContentType = parameterContent .keySet ().iterator ().next ();
395+ if (StringUtils .isNotBlank (encodingContentType )) {
396+ return Map .of (parameterInfo .getpName (), new Encoding ().contentType (encodingContentType ));
397+ }
398+ }
399+ }
400+ }
401+ return Map .of ();
402+ }
367403}
0 commit comments