35
35
import io .swagger .v3 .core .util .AnnotationsUtils ;
36
36
import io .swagger .v3 .oas .models .Components ;
37
37
import io .swagger .v3 .oas .models .media .Content ;
38
+ import io .swagger .v3 .oas .models .media .Encoding ;
38
39
import io .swagger .v3 .oas .models .media .MediaType ;
39
40
import io .swagger .v3 .oas .models .media .Schema ;
40
41
import io .swagger .v3 .oas .models .parameters .RequestBody ;
42
+ import jakarta .validation .constraints .NotNull ;
41
43
import org .apache .commons .lang3 .StringUtils ;
42
44
import org .springdoc .core .models .MethodAttributes ;
43
45
import org .springdoc .core .models .ParameterInfo ;
46
48
import org .springdoc .core .utils .SpringDocAnnotationsUtils ;
47
49
48
50
import org .springframework .core .MethodParameter ;
51
+ import org .springframework .util .CollectionUtils ;
49
52
import org .springframework .web .bind .annotation .RequestPart ;
50
53
51
54
import static org .springdoc .core .utils .SpringDocAnnotationsUtils .mergeSchema ;
@@ -146,9 +149,9 @@ public Optional<RequestBody> buildRequestBodyFromDoc(
146
149
* @param requestBodyObject the request body object
147
150
*/
148
151
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 ) {
152
155
Optional <Content > optionalContent = SpringDocAnnotationsUtils
153
156
.getContent (requestBody .content (), getConsumes (classConsumes ),
154
157
getConsumes (methodConsumes ), null , components , jsonViewAnnotation , parameterBuilder .isOpenapi31 ());
@@ -296,12 +299,14 @@ private RequestBody buildRequestBody(RequestBody requestBody, Components compone
296
299
if (requestBody .getContent () == null ) {
297
300
Schema <?> schema = parameterBuilder .calculateSchema (components , parameterInfo , requestBodyInfo ,
298
301
methodAttributes .getJsonViewAnnotationForRequestBody ());
299
- buildContent (requestBody , methodAttributes , schema );
302
+ Map <String , Encoding > parameterEncoding = getParameterEncoding (parameterInfo );
303
+ buildContent (requestBody , methodAttributes , schema , parameterEncoding );
300
304
}
301
305
else if (!methodAttributes .isWithResponseBodySchemaDoc ()) {
302
306
Schema <?> schema = parameterBuilder .calculateSchema (components , parameterInfo , requestBodyInfo ,
303
307
methodAttributes .getJsonViewAnnotationForRequestBody ());
304
- mergeContent (requestBody , methodAttributes , schema );
308
+ Map <String , Encoding > parameterEncoding = getParameterEncoding (parameterInfo );
309
+ mergeContent (requestBody , methodAttributes , schema , parameterEncoding );
305
310
}
306
311
307
312
// Add requestBody javadoc
@@ -318,38 +323,40 @@ else if (!methodAttributes.isWithResponseBodySchemaDoc()) {
318
323
/**
319
324
* Merge content.
320
325
*
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
324
329
*/
325
- private void mergeContent (RequestBody requestBody , MethodAttributes methodAttributes , Schema <?> schema ) {
330
+ private void mergeContent (RequestBody requestBody , MethodAttributes methodAttributes , Schema <?> schema , Map < String , Encoding > parameterEncoding ) {
326
331
Content content = requestBody .getContent ();
327
- buildContent (requestBody , methodAttributes , schema , content );
332
+ buildContent (requestBody , methodAttributes , schema , content , parameterEncoding );
328
333
}
329
334
330
335
/**
331
336
* Build content.
332
337
*
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
336
342
*/
337
- private void buildContent (RequestBody requestBody , MethodAttributes methodAttributes , Schema <?> schema ) {
343
+ private void buildContent (RequestBody requestBody , MethodAttributes methodAttributes , Schema <?> schema , Map < String , Encoding > parameterEncoding ) {
338
344
Content content = new Content ();
339
- buildContent (requestBody , methodAttributes , schema , content );
345
+ buildContent (requestBody , methodAttributes , schema , content , parameterEncoding );
340
346
}
341
347
342
348
/**
343
349
* Build content.
344
350
*
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
349
356
*/
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 ) {
351
358
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 ();
353
360
mediaTypeObject .setSchema (schema );
354
361
MediaType mediaType = content .get (value );
355
362
if (mediaType != null ) {
@@ -360,8 +367,37 @@ private void buildContent(RequestBody requestBody, MethodAttributes methodAttrib
360
367
if (mediaType .getEncoding () != null )
361
368
mediaTypeObject .setEncoding (mediaType .getEncoding ());
362
369
}
370
+ else if (!CollectionUtils .isEmpty (parameterEncoding )) {
371
+ mediaTypeObject .setEncoding (parameterEncoding );
372
+ }
363
373
content .addMediaType (value , mediaTypeObject );
364
374
}
365
375
requestBody .setContent (content );
366
376
}
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
+ }
367
403
}
0 commit comments