@@ -1696,108 +1696,112 @@ describe('SchemaModifier', () => {
16961696 } ) ;
16971697 } ) ;
16981698
1699- describe ( 'hoistAnyOfFromAllOf ' , ( ) => {
1700- it ( 'should move $ref variants into anyOf array ' , ( ) => {
1699+ describe ( 'normalizeAnyOfInAllOf ' , ( ) => {
1700+ it ( 'should replace anyOf item in allOf with merged properties object ' , ( ) => {
17011701 const doc = createDocument ( ) ;
17021702 doc . components ! . schemas = {
17031703 TestSchema : {
17041704 allOf : [
17051705 { $ref : '#/components/schemas/BaseSchema' } ,
17061706 {
17071707 anyOf : [
1708- { title : 'variant1 ' , $ref : '#/components/schemas/Variant1' } ,
1709- { title : 'variant2' , $ref : '#/components/schemas/Variant2' }
1708+ { title : 'lterms ' , $ref : '#/components/schemas/Variant1' } ,
1709+ { title : 'max' , $ref : '#/components/schemas/Variant2' }
17101710 ]
17111711 }
17121712 ]
1713- } ,
1714- BaseSchema : { type : 'object' } ,
1713+ } as any ,
1714+ BaseSchema : { type : 'object' , properties : { meta : { type : 'string' } } } ,
17151715 Variant1 : { type : 'object' } ,
17161716 Variant2 : { type : 'object' }
17171717 } ;
17181718
17191719 const modifier = new SchemaModifier ( doc ) as any ;
1720- const schema = doc . components ! . schemas ! . TestSchema as OpenAPIV3 . SchemaObject ;
1720+ const schema = doc . components ! . schemas ! . TestSchema as any ;
1721+ modifier . normalizeAnyOfInAllOf ( schema ) ;
17211722
1722- modifier . hoistAnyOfFromAllOf ( schema ) ;
1723+ // allOf is preserved with base ref untouched
1724+ expect ( schema . allOf ) . toBeDefined ( ) ;
1725+ expect ( schema . allOf ) . toHaveLength ( 2 ) ;
1726+ expect ( schema . allOf [ 0 ] ) . toEqual ( { $ref : '#/components/schemas/BaseSchema' } ) ;
1727+ // anyOf item replaced with merged properties object
1728+ expect ( schema . allOf [ 1 ] . anyOf ) . toBeUndefined ( ) ;
1729+ expect ( schema . allOf [ 1 ] . type ) . toBe ( 'object' ) ;
1730+ expect ( schema . allOf [ 1 ] . properties . lterms ) . toEqual ( { $ref : '#/components/schemas/Variant1' } ) ;
1731+ expect ( schema . allOf [ 1 ] . properties . max ) . toEqual ( { $ref : '#/components/schemas/Variant2' } ) ;
1732+ } ) ;
17231733
1724- expect ( schema . allOf ) . toBeUndefined ( ) ;
1725- expect ( schema . anyOf ) . toBeDefined ( ) ;
1726- expect ( schema . anyOf ) . toHaveLength ( 3 ) ;
1727- expect ( schema . anyOf ! [ 0 ] ) . toEqual ( { $ref : '#/components/schemas/BaseSchema' } ) ;
1728- expect ( schema . anyOf ! [ 1 ] ) . toEqual ( { title : 'variant1' , $ref : '#/components/schemas/Variant1' } ) ;
1729- expect ( schema . anyOf ! [ 2 ] ) . toEqual ( { title : 'variant2' , $ref : '#/components/schemas/Variant2' } ) ;
1734+ it ( 'should use type name snake_case for untitled variants mixed with titled ones' , ( ) => {
1735+ const doc = createDocument ( ) ;
1736+ doc . components ! . schemas = {
1737+ TestSchema : {
1738+ allOf : [
1739+ { $ref : '#/components/schemas/BaseSchema' } ,
1740+ {
1741+ anyOf : [
1742+ { title : 'lterms' , $ref : '#/components/schemas/LongTermsAggregate' } ,
1743+ { $ref : '#/components/schemas/MaxAggregate' } // no title
1744+ ]
1745+ }
1746+ ]
1747+ } as any ,
1748+ BaseSchema : { type : 'object' } ,
1749+ LongTermsAggregate : { type : 'object' } ,
1750+ MaxAggregate : { type : 'object' }
1751+ } ;
1752+
1753+ const modifier = new SchemaModifier ( doc ) as any ;
1754+ const schema = doc . components ! . schemas ! . TestSchema as any ;
1755+ modifier . normalizeAnyOfInAllOf ( schema ) ;
1756+
1757+ expect ( schema . allOf [ 1 ] . type ) . toBe ( 'object' ) ;
1758+ // titled variant uses its title
1759+ expect ( schema . allOf [ 1 ] . properties . lterms ) . toEqual ( { $ref : '#/components/schemas/LongTermsAggregate' } ) ;
1760+ // untitled variant falls back to snake_case of the type name
1761+ expect ( schema . allOf [ 1 ] . properties . max_aggregate ) . toEqual ( { $ref : '#/components/schemas/MaxAggregate' } ) ;
17301762 } ) ;
17311763
1732- it ( 'should NOT hoist anyOf when variants are inline objects (like field/script alternatives) ' , ( ) => {
1764+ it ( 'should not modify when variants have no titles ' , ( ) => {
17331765 const doc = createDocument ( ) ;
1734- doc . components ! . schemas ! . TermsAggregationFields = {
1766+ const original = {
17351767 allOf : [
1736- {
1737- type : 'object' ,
1738- properties : {
1739- collect_mode : { type : 'string' } ,
1740- min_doc_count : { type : 'integer' }
1741- }
1742- } ,
1743- {
1744- anyOf : [
1745- {
1746- type : 'object' ,
1747- properties : {
1748- field : { type : 'string' }
1749- }
1750- } ,
1751- {
1752- type : 'object' ,
1753- properties : {
1754- script : { type : 'string' }
1755- }
1756- }
1757- ]
1758- }
1768+ { $ref : '#/components/schemas/Base' } ,
1769+ { anyOf : [ { $ref : '#/components/schemas/V1' } , { $ref : '#/components/schemas/V2' } ] }
17591770 ]
17601771 } ;
1772+ doc . components ! . schemas ! . TestSchema = JSON . parse ( JSON . stringify ( original ) ) as any ;
17611773
17621774 const modifier = new SchemaModifier ( doc ) as any ;
1763- const schema = doc . components ! . schemas ! . TermsAggregationFields as OpenAPIV3 . SchemaObject ;
1764- const originalSchema = JSON . parse ( JSON . stringify ( schema ) ) ;
1765-
1766- modifier . hoistAnyOfFromAllOf ( schema ) ;
1775+ const schema = doc . components ! . schemas ! . TestSchema as any ;
1776+ modifier . normalizeAnyOfInAllOf ( schema ) ;
17671777
1768- // Should NOT modify schema when variants are inline objects
1769- expect ( schema ) . toEqual ( originalSchema ) ;
1770- expect ( schema . allOf ) . toBeDefined ( ) ;
1771- expect ( schema . anyOf ) . toBeUndefined ( ) ;
1778+ expect ( schema ) . toEqual ( original ) ;
17721779 } ) ;
17731780
1774- it ( 'should hoist even when $ref variants have additional properties like title ' , ( ) => {
1781+ it ( 'should not modify when allOf has no anyOf item ' , ( ) => {
17751782 const doc = createDocument ( ) ;
1776- doc . components ! . schemas = {
1777- Aggregate : {
1778- allOf : [
1779- { $ref : '#/components/schemas/AggregateBase' } ,
1780- {
1781- anyOf : [
1782- { title : 'adjacency_matrix' , $ref : '#/components/schemas/AdjacencyMatrixAggregate' } ,
1783- { title : 'avg' , $ref : '#/components/schemas/AvgAggregate' }
1784- ]
1785- }
1786- ]
1787- } ,
1788- AggregateBase : { type : 'object' } ,
1789- AdjacencyMatrixAggregate : { type : 'object' } ,
1790- AvgAggregate : { type : 'object' }
1791- } ;
1783+ doc . components ! . schemas ! . TestSchema = {
1784+ allOf : [ { $ref : '#/components/schemas/Base' } , { $ref : '#/components/schemas/Base2' } ]
1785+ } as any ;
17921786
17931787 const modifier = new SchemaModifier ( doc ) as any ;
1794- const schema = doc . components ! . schemas ! . Aggregate as OpenAPIV3 . SchemaObject ;
1788+ const schema = doc . components ! . schemas ! . TestSchema as any ;
1789+ const original = JSON . parse ( JSON . stringify ( schema ) ) ;
1790+ modifier . normalizeAnyOfInAllOf ( schema ) ;
17951791
1796- modifier . hoistAnyOfFromAllOf ( schema ) ;
1792+ expect ( schema ) . toEqual ( original ) ;
1793+ } ) ;
1794+
1795+ it ( 'should not modify schema without allOf' , ( ) => {
1796+ const doc = createDocument ( ) ;
1797+ doc . components ! . schemas ! . TestSchema = { type : 'object' , properties : { x : { type : 'string' } } } ;
1798+
1799+ const modifier = new SchemaModifier ( doc ) as any ;
1800+ const schema = doc . components ! . schemas ! . TestSchema as any ;
1801+ modifier . normalizeAnyOfInAllOf ( schema ) ;
17971802
17981803 expect ( schema . allOf ) . toBeUndefined ( ) ;
1799- expect ( schema . anyOf ) . toBeDefined ( ) ;
1800- expect ( schema . anyOf ) . toHaveLength ( 3 ) ;
1804+ expect ( schema . type ) . toBe ( 'object' ) ;
18011805 } ) ;
18021806 } ) ;
18031807} ) ;
0 commit comments