@@ -4,6 +4,7 @@ const fs = require('fs').promises
4
4
const path = require ( 'path' )
5
5
const sinon = require ( 'sinon' )
6
6
const $RefParser = require ( "@apidevtools/json-schema-ref-parser" )
7
+ const nock = require ( 'nock' )
7
8
const expect = require ( 'chai' ) . expect
8
9
9
10
const serverlessMock = require ( '../helpers/serverless' )
@@ -948,10 +949,13 @@ describe('DefinitionGenerator', () => {
948
949
949
950
describe ( 'schemas that are urls' , ( ) => {
950
951
it ( 'should attempt to download a schema and convert it' , async function ( ) {
951
- const simpleSchema = 'https:/// google.com/build/LicensedMember.json'
952
+ const simpleSchema = 'https://google.com/build/LicensedMember.json'
952
953
const LicensedMemberJSON = require ( '../json/complex.json' )
953
954
954
- const stub = sinon . stub ( $RefParser , 'dereference' ) . resolves ( LicensedMemberJSON )
955
+ const scope = nock ( 'https://google.com' )
956
+ . get ( '/build/LicensedMember.json' )
957
+ . reply ( 200 , LicensedMemberJSON )
958
+
955
959
const definitionGenerator = new DefinitionGenerator ( mockServerless )
956
960
const expected = await definitionGenerator . schemaCreator ( simpleSchema , 'LicensedMember' )
957
961
. catch ( ( err ) => {
@@ -963,28 +967,16 @@ describe('DefinitionGenerator', () => {
963
967
expect ( definitionGenerator . openAPI . components . schemas . LicensedMember . properties ) . to . have . property ( 'template' )
964
968
expect ( definitionGenerator . openAPI . components . schemas . LicensedMember . properties ) . to . have . property ( 'database' )
965
969
expect ( expected ) . to . equal ( '#/components/schemas/LicensedMember' )
966
-
967
- stub . restore ( )
968
970
} ) ;
969
971
970
972
it ( 'should take a mix of schemas' , async function ( ) {
971
- const complexSchema = 'https:/// google.com/build/LicensedMember.json'
973
+ const complexSchema = 'https://google.com/build/LicensedMember.json'
972
974
const LicensedMemberJSON = require ( '../json/complex.json' )
973
975
974
- // const stub = sinon.stub($RefParser, 'dereference').resolves(LicensedMemberJSON)
975
- const stub = sinon . stub ( $RefParser , 'dereference' )
976
- . onFirstCall ( ) . resolves ( LicensedMemberJSON )
977
- . onSecondCall ( ) . resolves ( {
978
- type : "object" ,
979
- properties : {
980
- UUID : {
981
- $ref : "#/definitions/log"
982
- } ,
983
- name : {
984
- type : "string"
985
- }
986
- }
987
- } )
976
+ const scope = nock ( 'https://google.com' )
977
+ . get ( '/build/LicensedMember.json' )
978
+ . reply ( 200 , LicensedMemberJSON )
979
+
988
980
const definitionGenerator = new DefinitionGenerator ( mockServerless )
989
981
let expected = await definitionGenerator . schemaCreator ( complexSchema , 'LicensedMember' )
990
982
. catch ( ( err ) => {
@@ -1007,35 +999,65 @@ describe('DefinitionGenerator', () => {
1007
999
1008
1000
expected = await definitionGenerator . schemaCreator ( simpleSchema , 'simpleSchema' )
1009
1001
. catch ( ( err ) => {
1002
+ expect ( err ) . to . be . an ( 'error' )
1010
1003
console . error ( err )
1011
1004
} )
1012
1005
1006
+ expect ( expected ) . to . be . undefined
1013
1007
expect ( definitionGenerator . openAPI . components . schemas ) . to . have . property ( 'LicensedMember' )
1014
1008
expect ( definitionGenerator . openAPI . components . schemas . LicensedMember . properties ) . to . have . property ( 'log' )
1015
1009
expect ( definitionGenerator . openAPI . components . schemas . LicensedMember . properties ) . to . have . property ( 'template' )
1016
1010
expect ( definitionGenerator . openAPI . components . schemas . LicensedMember . properties ) . to . have . property ( 'database' )
1017
- expect ( definitionGenerator . openAPI . components . schemas ) . to . have . property ( 'simpleSchema' )
1018
- expect ( definitionGenerator . openAPI . components . schemas . simpleSchema . properties ) . to . have . property ( 'UUID' )
1019
- expect ( definitionGenerator . openAPI . components . schemas . simpleSchema . properties ) . to . have . property ( 'name' )
1020
- expect ( expected ) . to . equal ( '#/components/schemas/simpleSchema' )
1021
-
1022
- stub . restore ( )
1023
1011
} ) ;
1024
1012
1025
1013
it ( 'should throw an error when a url can not be resolved' , async function ( ) {
1026
- const simpleSchema = 'https:///google.com/build/LicensedMember.json'
1014
+ const simpleSchema = 'https://google.com/build/LicensedMember.json'
1015
+
1016
+ const scope = nock ( 'https://google.com' )
1017
+ . get ( '/build/LicensedMember.json' )
1018
+ . reply ( 404 )
1027
1019
1028
- const stub = sinon . stub ( $RefParser , 'dereference' ) . rejects ( new Error ( ) )
1029
1020
const definitionGenerator = new DefinitionGenerator ( mockServerless )
1030
1021
const expected = await definitionGenerator . schemaCreator ( simpleSchema , 'simpleSchema' )
1031
1022
. catch ( ( err ) => {
1032
- console . error ( err )
1033
1023
expect ( err ) . to . be . an ( 'error' )
1034
1024
} )
1035
1025
1036
1026
expect ( expected ) . to . be . undefined
1027
+ } ) ;
1037
1028
1038
- stub . restore ( )
1029
+ it ( 'should handle a poorly dereferenced schema' , async function ( ) {
1030
+ const simpleSchema = 'https://google.com/build/LicensedMember.json'
1031
+
1032
+ const externalSchema = {
1033
+ $schema : 'http://json-schema.org/draft-04/schema#' ,
1034
+ title : 'JSON API Schema' ,
1035
+ $ref : '#/definitions/Error' ,
1036
+ definitions : {
1037
+ Error : {
1038
+ type : 'string'
1039
+ }
1040
+ }
1041
+ }
1042
+
1043
+ const scope = nock ( 'https://google.com' )
1044
+ . get ( '/build/LicensedMember.json' )
1045
+ . reply ( 200 , externalSchema )
1046
+
1047
+
1048
+ const definitionGenerator = new DefinitionGenerator ( mockServerless )
1049
+ const expected = await definitionGenerator . schemaCreator ( simpleSchema , 'LicensedMember' )
1050
+ . catch ( ( err ) => {
1051
+ console . error ( err )
1052
+ } )
1053
+
1054
+ expect ( definitionGenerator . openAPI . components . schemas ) . to . have . property ( 'LicensedMember' )
1055
+ expect ( definitionGenerator . openAPI . components . schemas . LicensedMember . properties ) . to . have . property ( 'Error' )
1056
+ expect ( definitionGenerator . openAPI . components . schemas . LicensedMember . properties . Error ) . to . have . property ( 'type' )
1057
+ expect ( definitionGenerator . openAPI . components . schemas . LicensedMember . properties . Error . type ) . to . be . equal ( 'string' )
1058
+ expect ( definitionGenerator . openAPI . components . schemas . LicensedMember ) . to . not . have . property ( '$schema' )
1059
+ expect ( definitionGenerator . openAPI . components . schemas . LicensedMember ) . to . not . have . property ( '$definitions' )
1060
+ expect ( expected ) . to . equal ( '#/components/schemas/LicensedMember' )
1039
1061
} ) ;
1040
1062
} ) ;
1041
1063
} ) ;
0 commit comments