@@ -9,6 +9,7 @@ const path = require("path");
99
1010const serverlessMock = require ( "../helpers/serverless" ) ;
1111const modelsDocument = require ( "../models/models/models.json" ) ;
12+ const alternativeModelsDocument = require ( "../models/models/models-alt.json" ) ;
1213
1314const schemaHandler = require ( '../../src/schemaHandler' ) ;
1415
@@ -1255,5 +1256,89 @@ describe("DefinitionGenerator", () => {
12551256 expect ( response [ "200" ] . headers ) . to . have . property ( "x-rate-limit" ) ;
12561257 } ) ;
12571258 } ) ;
1259+
1260+ describe ( `mediaTypeObjects` , function ( ) {
1261+ describe ( `content style` , function ( ) {
1262+ it ( `should add examples when examples are specified` , async function ( ) {
1263+ const modelsWithExample = structuredClone ( modelsDocument ) ;
1264+ modelsWithExample . models . at ( 0 ) . content [ "application/json" ] . examples = [ { name : '404Error' , summary : 'an example of a 404 error' , description : 'This is what a 404 error looks like' , value : '404' } ]
1265+ Object . assign ( mockServerless . service . custom . documentation , modelsWithExample ) ;
1266+
1267+ const description = "this is a description" ;
1268+ const responseMock = {
1269+ methodResponses : [
1270+ {
1271+ responseBody : { description : description } ,
1272+ responseModels : { 'application/json' : 'ErrorResponse' } ,
1273+ statusCode : 200 ,
1274+ } ,
1275+ ] ,
1276+ } ;
1277+
1278+ const stub = sinon . stub ( schemaHandler . prototype , 'createSchema' ) . resolves ( '#components/schemas/ErrorResponse' )
1279+
1280+ const definitionGenerator = new DefinitionGenerator (
1281+ mockServerless ,
1282+ logger
1283+ ) ;
1284+
1285+ const response = await definitionGenerator . createResponses ( responseMock ) ;
1286+
1287+ expect ( response ) . to . be . an ( "object" ) ;
1288+ expect ( response ) . to . have . property ( "200" ) ;
1289+ expect ( response [ "200" ] ) . to . have . property ( 'content' )
1290+ expect ( response [ "200" ] . content ) . to . be . an ( 'object' )
1291+ expect ( response [ "200" ] . content ) . to . have . property ( 'application/json' )
1292+ expect ( response [ "200" ] . content [ 'application/json' ] ) . to . be . an ( 'object' )
1293+ expect ( response [ "200" ] . content [ 'application/json' ] ) . to . have . property ( 'examples' )
1294+ expect ( response [ "200" ] . content [ 'application/json' ] . examples ) . to . be . an ( 'object' )
1295+ expect ( response [ "200" ] . content [ 'application/json' ] . examples ) . to . have . property ( '404Error' )
1296+
1297+
1298+ stub . restore ( )
1299+ } ) ;
1300+ } ) ;
1301+
1302+ describe ( `contentType style` , function ( ) {
1303+ it ( `should add examples when examples are specified` , async function ( ) {
1304+ const altModelsWithExample = structuredClone ( alternativeModelsDocument ) ;
1305+ altModelsWithExample . models . at ( 0 ) . examples = [ { name : '404Error' , summary : 'an example of a 404 error' , description : 'This is what a 404 error looks like' , value : '404' } ]
1306+ Object . assign ( mockServerless . service . custom . documentation , altModelsWithExample ) ;
1307+
1308+ const description = "this is a description" ;
1309+ const responseMock = {
1310+ methodResponses : [
1311+ {
1312+ responseBody : { description : description } ,
1313+ responseModels : { 'application/json' : 'ErrorResponse' } ,
1314+ statusCode : 200 ,
1315+ } ,
1316+ ] ,
1317+ } ;
1318+
1319+ const stub = sinon . stub ( schemaHandler . prototype , 'createSchema' ) . resolves ( '#components/schemas/ErrorResponse' )
1320+
1321+ const definitionGenerator = new DefinitionGenerator (
1322+ mockServerless ,
1323+ logger
1324+ ) ;
1325+
1326+
1327+ const response = await definitionGenerator . createResponses ( responseMock ) ;
1328+
1329+ expect ( response ) . to . be . an ( "object" ) ;
1330+ expect ( response ) . to . have . property ( "200" ) ;
1331+ expect ( response [ "200" ] ) . to . have . property ( 'content' )
1332+ expect ( response [ "200" ] . content ) . to . be . an ( 'object' )
1333+ expect ( response [ "200" ] . content ) . to . have . property ( 'application/json' )
1334+ expect ( response [ "200" ] . content [ 'application/json' ] ) . to . be . an ( 'object' )
1335+ expect ( response [ "200" ] . content [ 'application/json' ] ) . to . have . property ( 'examples' )
1336+ expect ( response [ "200" ] . content [ 'application/json' ] . examples ) . to . be . an ( 'object' )
1337+ expect ( response [ "200" ] . content [ 'application/json' ] . examples ) . to . have . property ( '404Error' )
1338+
1339+ stub . restore ( )
1340+ } ) ;
1341+ } ) ;
1342+ } ) ;
12581343 } ) ;
12591344} ) ;
0 commit comments