@@ -700,32 +700,92 @@ public void simplifyAnyOfWithOnlyOneNonNullSubSchemaKeepsReadOnlyWriteOnlyAttrib
700700 public void simplifyOneOfAnyOfWithOnlyOneNonNullSubSchemaKeepsParentDescription () {
701701 OpenAPI openAPI = new OpenAPI ();
702702
703- Schema anyOfParent = new Schema (). description ( "Access token" );
704- anyOfParent . setAnyOf ( new ArrayList <>( Arrays . asList (
703+ Schema anyOfParent = anyOfSchema (
704+ "Access token" ,
705705 new StringSchema (),
706706 new Schema <>().type ("null" )
707- ))) ;
707+ );
708708 Schema anyOfSchema = ModelUtils .simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema (openAPI , anyOfParent , anyOfParent .getAnyOf ());
709709 assertEquals (anyOfSchema .getDescription (), "Access token" );
710710
711- Schema oneOfParent = new Schema (). description ( "Expires at" );
712- oneOfParent . setOneOf ( new ArrayList <>( Arrays . asList (
711+ Schema oneOfParent = oneOfSchema (
712+ "Expires at" ,
713713 new IntegerSchema (),
714714 new Schema <>().type ("null" )
715- ))) ;
715+ );
716716 Schema oneOfSchema = ModelUtils .simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema (openAPI , oneOfParent , oneOfParent .getOneOf ());
717717 assertEquals (oneOfSchema .getDescription (), "Expires at" );
718718
719- Schema anyOfParentWithChildDescription = new Schema (). description ( "Parent description" );
720- anyOfParentWithChildDescription . setAnyOf ( new ArrayList <>( Arrays . asList (
719+ Schema anyOfParentWithChildDescription = anyOfSchema (
720+ "Parent description" ,
721721 new StringSchema ().description ("Child description" ),
722722 new Schema <>().type ("null" )
723- ))) ;
723+ );
724724 Schema anyOfSchemaWithChildDescription = ModelUtils .simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema (
725725 openAPI ,
726726 anyOfParentWithChildDescription ,
727727 anyOfParentWithChildDescription .getAnyOf ());
728728 assertEquals (anyOfSchemaWithChildDescription .getDescription (), "Child description" );
729+
730+ Schema oneOfParentWithChildDescription = oneOfSchema (
731+ "Parent description" ,
732+ new StringSchema ().description ("Child description" ),
733+ new Schema <>().type ("null" )
734+ );
735+ Schema oneOfSchemaWithChildDescription = ModelUtils .simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema (
736+ openAPI ,
737+ oneOfParentWithChildDescription ,
738+ oneOfParentWithChildDescription .getOneOf ());
739+ assertEquals (oneOfSchemaWithChildDescription .getDescription (), "Child description" );
740+ }
741+
742+ @ Test
743+ public void simplifySingleRefOneOfAnyOfDropsParentDescriptionToKeepAlias () {
744+ OpenAPI openAPI = new OpenAPI ();
745+
746+ Schema refSchema = new Schema ().$ref ("#/components/schemas/IntegerRef" );
747+
748+ Schema oneOfParent = oneOfSchema ("Expires at" , refSchema );
749+ Schema oneOfSchema = ModelUtils .simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema (openAPI , oneOfParent , oneOfParent .getOneOf ());
750+
751+ assertNull (oneOfSchema .getDescription ());
752+ assertEquals (oneOfSchema .get$ref (), "#/components/schemas/IntegerRef" );
753+
754+ Schema anyOfParent = anyOfSchema ("Expires at" , refSchema );
755+ Schema anyOfSchema = ModelUtils .simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema (openAPI , anyOfParent , anyOfParent .getAnyOf ());
756+
757+ assertNull (anyOfSchema .getDescription ());
758+ assertEquals (anyOfSchema .get$ref (), "#/components/schemas/IntegerRef" );
759+
760+ Schema nullableOneOfParent = oneOfSchema ("Expires at" , refSchema );
761+ nullableOneOfParent .setNullable (true );
762+ Schema nullableOneOfSchema = ModelUtils .simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema (openAPI , nullableOneOfParent , nullableOneOfParent .getOneOf ());
763+
764+ assertEquals (nullableOneOfSchema .getDescription (), "Expires at" );
765+ assertEquals (nullableOneOfSchema .get$ref (), "#/components/schemas/IntegerRef" );
766+
767+ Schema nullableAnyOfParent = anyOfSchema ("Expires at" , refSchema );
768+ nullableAnyOfParent .setNullable (true );
769+ Schema nullableAnyOfSchema = ModelUtils .simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema (openAPI , nullableAnyOfParent , nullableAnyOfParent .getAnyOf ());
770+
771+ assertEquals (nullableAnyOfSchema .getDescription (), "Expires at" );
772+ assertEquals (nullableAnyOfSchema .get$ref (), "#/components/schemas/IntegerRef" );
773+ }
774+
775+ @ Test
776+ public void simplifySingleInlineOneOfAnyOfKeepsParentDescription () {
777+ OpenAPI openAPI = new OpenAPI ();
778+
779+ Schema oneOfParent = oneOfSchema ("Expires at" );
780+ Schema oneOfSchema = ModelUtils .simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema (openAPI , oneOfParent , oneOfParent .getOneOf ());
781+
782+ assertEquals (oneOfSchema .getDescription (), "Expires at" );
783+ assertNull (oneOfSchema .get$ref ());
784+
785+ Schema anyOfParent = anyOfSchema ("Expires at" );
786+ Schema anyOfSchema = ModelUtils .simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema (openAPI , anyOfParent , anyOfParent .getAnyOf ());
787+
788+ assertEquals (anyOfSchema .getDescription (), "Expires at" );
729789 }
730790
731791 @ Test
@@ -1427,4 +1487,28 @@ private static Schema<?> nullableStringSchema() {
14271487 return schema ;
14281488 }
14291489
1490+ private static Schema <?> oneOfSchema (String description ) {
1491+ return oneOfSchema (description , new IntegerSchema ());
1492+ }
1493+
1494+ private static Schema <?> oneOfSchema (String description , Schema <?>... alternatives ) {
1495+ Schema <?> oneOfSchema = new Schema <>().description (description );
1496+ oneOfSchema .setOneOf (new ArrayList <>(Arrays .asList (
1497+ alternatives
1498+ )));
1499+ return oneOfSchema ;
1500+ }
1501+
1502+ private static Schema <?> anyOfSchema (String description ) {
1503+ return anyOfSchema (description , new IntegerSchema ());
1504+ }
1505+
1506+ private static Schema <?> anyOfSchema (String description , Schema <?>... alternatives ) {
1507+ Schema <?> anyOfSchema = new Schema <>().description (description );
1508+ anyOfSchema .setAnyOf (new ArrayList <>(Arrays .asList (
1509+ alternatives
1510+ )));
1511+ return anyOfSchema ;
1512+ }
1513+
14301514}
0 commit comments