10
10
import io .swagger .v3 .core .converter .ModelConverters ;
11
11
import io .swagger .v3 .core .converter .ResolvedSchema ;
12
12
import io .swagger .v3 .oas .annotations .StringToClassMapItem ;
13
+ import io .swagger .v3 .oas .annotations .enums .Explode ;
13
14
import io .swagger .v3 .oas .annotations .extensions .Extension ;
14
15
import io .swagger .v3 .oas .annotations .extensions .ExtensionProperty ;
15
16
import io .swagger .v3 .oas .annotations .links .LinkParameter ;
41
42
import org .apache .commons .lang3 .math .NumberUtils ;
42
43
import org .slf4j .Logger ;
43
44
import org .slf4j .LoggerFactory ;
44
-
45
45
import java .io .IOException ;
46
46
import java .lang .annotation .Annotation ;
47
47
import java .lang .reflect .Field ;
@@ -1288,17 +1288,24 @@ public static Map<String, String> getLinkParameters(LinkParameter[] linkParamete
1288
1288
}
1289
1289
1290
1290
public static Optional <Map <String , Header >> getHeaders (io .swagger .v3 .oas .annotations .headers .Header [] annotationHeaders , JsonView jsonViewAnnotation ) {
1291
- return getHeaders (annotationHeaders , jsonViewAnnotation , false );
1291
+ return getHeaders (annotationHeaders , null , jsonViewAnnotation );
1292
+ }
1293
+ public static Optional <Map <String , Header >> getHeaders (io .swagger .v3 .oas .annotations .headers .Header [] annotationHeaders , Components components , JsonView jsonViewAnnotation ) {
1294
+ return getHeaders (annotationHeaders , components , jsonViewAnnotation , false );
1292
1295
}
1293
1296
1294
1297
public static Optional <Map <String , Header >> getHeaders (io .swagger .v3 .oas .annotations .headers .Header [] annotationHeaders , JsonView jsonViewAnnotation , boolean openapi31 ) {
1298
+ return getHeaders (annotationHeaders , null , jsonViewAnnotation , openapi31 );
1299
+ }
1300
+
1301
+ public static Optional <Map <String , Header >> getHeaders (io .swagger .v3 .oas .annotations .headers .Header [] annotationHeaders , Components components , JsonView jsonViewAnnotation , boolean openapi31 ) {
1295
1302
if (annotationHeaders == null ) {
1296
1303
return Optional .empty ();
1297
1304
}
1298
1305
1299
1306
Map <String , Header > headers = new HashMap <>();
1300
1307
for (io .swagger .v3 .oas .annotations .headers .Header header : annotationHeaders ) {
1301
- getHeader (header , jsonViewAnnotation ).ifPresent (headerResult -> headers .put (header .name (), headerResult ));
1308
+ getHeader (header , components , jsonViewAnnotation , openapi31 ).ifPresent (headerResult -> headers .put (header .name (), headerResult ));
1302
1309
}
1303
1310
1304
1311
if (headers .size () == 0 ) {
@@ -1308,12 +1315,18 @@ public static Optional<Map<String, Header>> getHeaders(io.swagger.v3.oas.annotat
1308
1315
}
1309
1316
1310
1317
public static Optional <Header > getHeader (io .swagger .v3 .oas .annotations .headers .Header header , JsonView jsonViewAnnotation ) {
1311
- return getHeader (header , jsonViewAnnotation , false );
1318
+ return getHeader (header , null , jsonViewAnnotation );
1319
+ }
1320
+ public static Optional <Header > getHeader (io .swagger .v3 .oas .annotations .headers .Header header , Components components , JsonView jsonViewAnnotation ) {
1321
+ return getHeader (header , components , jsonViewAnnotation , false );
1312
1322
}
1313
1323
1314
1324
public static Optional <Header > getHeader (io .swagger .v3 .oas .annotations .headers .Header header , JsonView jsonViewAnnotation , boolean openapi31 ) {
1325
+ return getHeader (header , null , jsonViewAnnotation , openapi31 );
1326
+ }
1327
+ public static Optional <Header > getHeader (io .swagger .v3 .oas .annotations .headers .Header header , Components components , JsonView jsonViewAnnotation , boolean openapi31 ) {
1315
1328
1316
- if (header == null ) {
1329
+ if (header == null || header . hidden () ) {
1317
1330
return Optional .empty ();
1318
1331
}
1319
1332
@@ -1327,29 +1340,90 @@ public static Optional<Header> getHeader(io.swagger.v3.oas.annotations.headers.H
1327
1340
headerObject .set$ref (header .ref ());
1328
1341
isEmpty = false ;
1329
1342
}
1343
+ if (StringUtils .isNotBlank (header .example ())) {
1344
+ try {
1345
+ headerObject .setExample (Json .mapper ().readTree (header .example ()));
1346
+ } catch (IOException e ) {
1347
+ headerObject .setExample (header .example ());
1348
+ }
1349
+ }
1330
1350
if (header .deprecated ()) {
1331
1351
headerObject .setDeprecated (header .deprecated ());
1332
1352
}
1333
1353
if (header .required ()) {
1334
1354
headerObject .setRequired (header .required ());
1335
1355
isEmpty = false ;
1336
1356
}
1357
+ Map <String , Example > exampleMap = new LinkedHashMap <>();
1358
+ if (header .examples ().length == 1 && StringUtils .isBlank (header .examples ()[0 ].name ())) {
1359
+ Optional <Example > exampleOptional = AnnotationsUtils .getExample (header .examples ()[0 ], true );
1360
+ exampleOptional .ifPresent (headerObject ::setExample );
1361
+ } else {
1362
+ for (ExampleObject exampleObject : header .examples ()) {
1363
+ AnnotationsUtils .getExample (exampleObject ).ifPresent (example -> exampleMap .put (exampleObject .name (), example ));
1364
+ }
1365
+ }
1366
+ if (!exampleMap .isEmpty ()) {
1367
+ headerObject .setExamples (exampleMap );
1368
+ }
1337
1369
headerObject .setStyle (Header .StyleEnum .SIMPLE );
1338
1370
1339
1371
if (header .schema () != null ) {
1340
1372
if (header .schema ().implementation ().equals (Void .class )) {
1341
1373
AnnotationsUtils .getSchemaFromAnnotation (header .schema (), jsonViewAnnotation , openapi31 ).ifPresent (
1342
1374
headerObject ::setSchema );
1375
+ }else {
1376
+ AnnotatedType annotatedType = new AnnotatedType ()
1377
+ .type (getSchemaType (header .schema ()))
1378
+ .resolveAsRef (true )
1379
+ .skipOverride (true )
1380
+ .jsonViewAnnotation (jsonViewAnnotation );
1381
+
1382
+ final ResolvedSchema resolvedSchema = ModelConverters .getInstance (openapi31 ).resolveAsResolvedSchema (annotatedType );
1383
+
1384
+ if (resolvedSchema .schema != null ) {
1385
+ headerObject .setSchema (resolvedSchema .schema );
1386
+ }
1387
+ resolvedSchema .referencedSchemas .forEach (components ::addSchemas );
1343
1388
}
1344
1389
}
1390
+ if (hasArrayAnnotation (header .array ())){
1391
+ AnnotationsUtils .getArraySchema (header .array (), components , jsonViewAnnotation , openapi31 , null , true ).ifPresent (
1392
+ headerObject ::setSchema );
1393
+ }
1345
1394
1395
+ setHeaderExplode (headerObject , header );
1346
1396
if (isEmpty ) {
1347
1397
return Optional .empty ();
1348
1398
}
1349
1399
1350
1400
return Optional .of (headerObject );
1351
1401
}
1352
1402
1403
+ public static void setHeaderExplode (Header header , io .swagger .v3 .oas .annotations .headers .Header h ) {
1404
+ if (isHeaderExplodable (h , header )) {
1405
+ if (Explode .TRUE .equals (h .explode ())) {
1406
+ header .setExplode (Boolean .TRUE );
1407
+ } else if (Explode .FALSE .equals (h .explode ())) {
1408
+ header .setExplode (Boolean .FALSE );
1409
+ }
1410
+ }
1411
+ }
1412
+
1413
+ private static boolean isHeaderExplodable (io .swagger .v3 .oas .annotations .headers .Header h , Header header ) {
1414
+ io .swagger .v3 .oas .annotations .media .Schema schema = h .schema ();
1415
+ boolean explode = true ;
1416
+ if (schema != null ) {
1417
+ Class implementation = schema .implementation ();
1418
+ if (implementation == Void .class ) {
1419
+ if (!schema .type ().equals ("object" ) && !schema .type ().equals ("array" )) {
1420
+ explode = false ;
1421
+ }
1422
+ }
1423
+ }
1424
+ return explode ;
1425
+ }
1426
+
1353
1427
public static void addEncodingToMediaType (MediaType mediaType , io .swagger .v3 .oas .annotations .media .Encoding encoding , JsonView jsonViewAnnotation ) {
1354
1428
addEncodingToMediaType (mediaType , encoding , jsonViewAnnotation , false );
1355
1429
}
@@ -1376,7 +1450,7 @@ public static void addEncodingToMediaType(MediaType mediaType, io.swagger.v3.oas
1376
1450
}
1377
1451
1378
1452
if (encoding .headers () != null ) {
1379
- getHeaders (encoding .headers (), jsonViewAnnotation , openapi31 ).ifPresent (encodingObject ::headers );
1453
+ getHeaders (encoding .headers (), null , jsonViewAnnotation , openapi31 ).ifPresent (encodingObject ::headers );
1380
1454
}
1381
1455
if (encoding .extensions () != null && encoding .extensions ().length > 0 ) {
1382
1456
Map <String , Object > extensions = AnnotationsUtils .getExtensions (openapi31 , encoding .extensions ());
0 commit comments