1010import io .swagger .v3 .core .converter .ModelConverters ;
1111import io .swagger .v3 .core .converter .ResolvedSchema ;
1212import io .swagger .v3 .oas .annotations .StringToClassMapItem ;
13+ import io .swagger .v3 .oas .annotations .enums .Explode ;
1314import io .swagger .v3 .oas .annotations .extensions .Extension ;
1415import io .swagger .v3 .oas .annotations .extensions .ExtensionProperty ;
1516import io .swagger .v3 .oas .annotations .links .LinkParameter ;
4142import org .apache .commons .lang3 .math .NumberUtils ;
4243import org .slf4j .Logger ;
4344import org .slf4j .LoggerFactory ;
44-
4545import java .io .IOException ;
4646import java .lang .annotation .Annotation ;
4747import java .lang .reflect .Field ;
@@ -1288,17 +1288,24 @@ public static Map<String, String> getLinkParameters(LinkParameter[] linkParamete
12881288 }
12891289
12901290 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 );
12921295 }
12931296
12941297 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 ) {
12951302 if (annotationHeaders == null ) {
12961303 return Optional .empty ();
12971304 }
12981305
12991306 Map <String , Header > headers = new HashMap <>();
13001307 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 ));
13021309 }
13031310
13041311 if (headers .size () == 0 ) {
@@ -1308,12 +1315,18 @@ public static Optional<Map<String, Header>> getHeaders(io.swagger.v3.oas.annotat
13081315 }
13091316
13101317 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 );
13121322 }
13131323
13141324 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 ) {
13151328
1316- if (header == null ) {
1329+ if (header == null || header . hidden () ) {
13171330 return Optional .empty ();
13181331 }
13191332
@@ -1327,29 +1340,90 @@ public static Optional<Header> getHeader(io.swagger.v3.oas.annotations.headers.H
13271340 headerObject .set$ref (header .ref ());
13281341 isEmpty = false ;
13291342 }
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+ }
13301350 if (header .deprecated ()) {
13311351 headerObject .setDeprecated (header .deprecated ());
13321352 }
13331353 if (header .required ()) {
13341354 headerObject .setRequired (header .required ());
13351355 isEmpty = false ;
13361356 }
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+ }
13371369 headerObject .setStyle (Header .StyleEnum .SIMPLE );
13381370
13391371 if (header .schema () != null ) {
13401372 if (header .schema ().implementation ().equals (Void .class )) {
13411373 AnnotationsUtils .getSchemaFromAnnotation (header .schema (), jsonViewAnnotation , openapi31 ).ifPresent (
13421374 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 );
13431388 }
13441389 }
1390+ if (hasArrayAnnotation (header .array ())){
1391+ AnnotationsUtils .getArraySchema (header .array (), components , jsonViewAnnotation , openapi31 , null , true ).ifPresent (
1392+ headerObject ::setSchema );
1393+ }
13451394
1395+ setHeaderExplode (headerObject , header );
13461396 if (isEmpty ) {
13471397 return Optional .empty ();
13481398 }
13491399
13501400 return Optional .of (headerObject );
13511401 }
13521402
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+
13531427 public static void addEncodingToMediaType (MediaType mediaType , io .swagger .v3 .oas .annotations .media .Encoding encoding , JsonView jsonViewAnnotation ) {
13541428 addEncodingToMediaType (mediaType , encoding , jsonViewAnnotation , false );
13551429 }
@@ -1376,7 +1450,7 @@ public static void addEncodingToMediaType(MediaType mediaType, io.swagger.v3.oas
13761450 }
13771451
13781452 if (encoding .headers () != null ) {
1379- getHeaders (encoding .headers (), jsonViewAnnotation , openapi31 ).ifPresent (encodingObject ::headers );
1453+ getHeaders (encoding .headers (), null , jsonViewAnnotation , openapi31 ).ifPresent (encodingObject ::headers );
13801454 }
13811455 if (encoding .extensions () != null && encoding .extensions ().length > 0 ) {
13821456 Map <String , Object > extensions = AnnotationsUtils .getExtensions (openapi31 , encoding .extensions ());
0 commit comments