@@ -16,20 +16,20 @@ const { detect } = adapter;
1616
1717fury . adapters = [ adapter ] ;
1818
19- function testFixture ( description , fixture , generateSourceMap = false ) {
19+ function testFixture ( description , fixture , adapterOptions ) {
2020 it ( description , ( done ) => {
2121 const source = fixture . swagger ;
2222 let expected ;
2323
24- if ( generateSourceMap ) {
24+ if ( adapterOptions && adapterOptions . generateSourceMap ) {
2525 expected = fixture . apiElementsSourceMap ;
2626 } else {
2727 expected = fixture . apiElements ;
2828 }
2929
3030 const mediaType = 'application/swagger+yaml' ;
3131
32- fury . parse ( { source, mediaType, generateSourceMap } , ( err , output ) => {
32+ fury . parse ( { source, mediaType, adapterOptions } , ( err , output ) => {
3333 if ( err && ! output ) {
3434 return done ( err ) ;
3535 }
@@ -40,7 +40,7 @@ function testFixture(description, fixture, generateSourceMap = false) {
4040 if ( process . env . GENERATE ) {
4141 expected = fury . minim . toRefract ( output ) ;
4242
43- if ( generateSourceMap ) {
43+ if ( adapterOptions && adapterOptions . generateSourceMap ) {
4444 // eslint-disable-next-line no-param-reassign
4545 fixture . apiElementsSourceMap = expected ;
4646 } else {
@@ -55,6 +55,24 @@ function testFixture(description, fixture, generateSourceMap = false) {
5555 } ) ;
5656}
5757
58+ function testFixtureOptions ( swaggerPath , apiElementsPath ) {
59+ const swagger = fs . readFileSync ( swaggerPath , 'utf-8' ) ;
60+ const options = { swagger } ;
61+
62+ Object . defineProperty ( options , 'apiElements' , {
63+ get ( ) {
64+ return require ( apiElementsPath ) ;
65+ } ,
66+
67+ set ( value ) {
68+ fs . writeFileSync ( apiElementsPath , JSON . stringify ( value , null , 2 ) ) ;
69+ return value ;
70+ } ,
71+ } ) ;
72+
73+ return options ;
74+ }
75+
5876describe ( 'Swagger 2.0 adapter' , ( ) => {
5977 context ( 'detection' , ( ) => {
6078 it ( 'detects JSON' , ( ) => {
@@ -141,17 +159,17 @@ describe('Swagger 2.0 adapter', () => {
141159 const fixtures = swaggerZoo . features ( ) ;
142160 fixtures . forEach ( ( fixture ) => {
143161 testFixture ( `Parses ${ fixture . name } ` , fixture ) ;
144- testFixture ( `Parses ${ fixture . name } with source maps` , fixture , true ) ;
162+ testFixture ( `Parses ${ fixture . name } with source maps` , fixture , { generateSourceMap : true } ) ;
145163 } ) ;
146164 } ) ;
147165
148166 describe ( 'can parse regression fixtures' , ( ) => {
149167 const files = glob . sync ( path . join ( __dirname , 'fixtures' , '*.yaml' ) ) ;
150168
151- files . forEach ( ( file ) => {
152- const name = path . basename ( file , path . extname ( file ) ) ;
169+ files . forEach ( ( swaggerPath ) => {
170+ const name = path . basename ( swaggerPath , path . extname ( swaggerPath ) ) ;
153171
154- const swagger = fs . readFileSync ( file , 'utf-8' ) ;
172+ const swagger = fs . readFileSync ( swaggerPath , 'utf-8' ) ;
155173 const apiElementsPath = path . join ( __dirname , 'fixtures' , `${ name } .json` ) ;
156174 const apiElementsSourceMapPath = path . join ( __dirname , 'fixtures' , `${ name } .sourcemap.json` ) ;
157175
@@ -181,6 +199,26 @@ describe('Swagger 2.0 adapter', () => {
181199
182200 testFixture ( `Parses ${ name } ` , options ) ;
183201 testFixture ( `Parses ${ name } with source maps` , options , true ) ;
202+ testFixture ( `Parses ${ name } ` , testFixtureOptions ( swaggerPath , apiElementsPath ) ) ;
203+ } ) ;
204+ } ) ;
205+
206+ describe ( '#adapterOptions' , ( ) => {
207+ describe ( '#generateMessageBody' , ( ) => {
208+ testFixture ( 'generates message body by default' , testFixtureOptions (
209+ path . join ( __dirname , 'fixtures' , 'json-body-generation.yaml' ) ,
210+ path . join ( __dirname , 'fixtures' , 'options' , 'generateMessageBody-true.json' )
211+ ) ) ;
212+
213+ testFixture ( 'generates message body when generateMessageBody is true' , testFixtureOptions (
214+ path . join ( __dirname , 'fixtures' , 'json-body-generation.yaml' ) ,
215+ path . join ( __dirname , 'fixtures' , 'options' , 'generateMessageBody-true.json' )
216+ ) , { generateMessageBody : true } ) ;
217+
218+ testFixture ( 'disables generating message body when requested' , testFixtureOptions (
219+ path . join ( __dirname , 'fixtures' , 'json-body-generation.yaml' ) ,
220+ path . join ( __dirname , 'fixtures' , 'options' , 'generateMessageBody-false.json' )
221+ ) , { generateMessageBody : false } ) ;
184222 } ) ;
185223 } ) ;
186224} ) ;
0 commit comments