Skip to content

Commit 446ffa3

Browse files
authored
Merge pull request #86 from JaredCE/fix-schema-references
Correctly deal with poorly dereferenced schemas
2 parents 4f9f575 + 1ffb3a3 commit 446ffa3

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-openapi-documenter",
3-
"version": "0.0.43",
3+
"version": "0.0.44",
44
"description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
55
"main": "index.js",
66
"keywords": [

src/definitionGenerator.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,10 @@ class DefinitionGenerator {
479479
const path = oldRef.split('/')
480480

481481
const pathTitle = path[path.length-1]
482-
const property = deReferencedSchema.definitions[path[path.length-1]]
483-
Object.assign(deReferencedSchema, {properties: {[pathTitle]: property}})
482+
const referencedProperties = deReferencedSchema.definitions[pathTitle]
483+
484+
Object.assign(deReferencedSchema, {...referencedProperties})
485+
484486
delete deReferencedSchema.$ref
485487
deReferencedSchema = await this.dereferenceSchema(deReferencedSchema)
486488
.catch((err) => {

test/unit/definitionGenerator.spec.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -887,9 +887,52 @@ describe('DefinitionGenerator', () => {
887887
})
888888

889889
expect(definitionGenerator.openAPI.components.schemas).to.have.property('main')
890-
expect(definitionGenerator.openAPI.components.schemas.main.properties).to.have.property('Error')
891-
expect(definitionGenerator.openAPI.components.schemas.main.properties.Error).to.have.property('type')
892-
expect(definitionGenerator.openAPI.components.schemas.main.properties.Error.type).to.be.equal('string')
890+
expect(definitionGenerator.openAPI.components.schemas.main).to.have.property('type')
891+
expect(definitionGenerator.openAPI.components.schemas.main.type).to.be.equal('string')
892+
expect(definitionGenerator.openAPI.components.schemas.main).to.not.have.property('$schema')
893+
expect(definitionGenerator.openAPI.components.schemas.main).to.not.have.property('$definitions')
894+
expect(expected).to.equal('#/components/schemas/main')
895+
896+
expect(spy.callCount).to.be.equal(2)
897+
898+
spy.resetHistory()
899+
});
900+
901+
it('should handle a complex schema that has been incorrectly dereferenced', async function() {
902+
const definitionGenerator = new DefinitionGenerator(mockServerless)
903+
904+
const complexSchema = {
905+
$schema: 'http://json-schema.org/draft-04/schema#',
906+
title: 'JSON API Schema',
907+
$ref: '#/definitions/Person',
908+
definitions: {
909+
Person: {
910+
type: 'object',
911+
properties: {
912+
name: {
913+
type: 'string'
914+
},
915+
age: {
916+
type: 'number'
917+
}
918+
}
919+
}
920+
}
921+
}
922+
923+
const spy = sinon.spy(definitionGenerator, 'dereferenceSchema')
924+
925+
const expected = await definitionGenerator.schemaCreator(complexSchema, 'main')
926+
.catch((err) => {
927+
console.error(err)
928+
})
929+
930+
expect(definitionGenerator.openAPI.components.schemas).to.have.property('main')
931+
expect(definitionGenerator.openAPI.components.schemas.main).to.have.property('type')
932+
expect(definitionGenerator.openAPI.components.schemas.main.type).to.be.equal('object')
933+
expect(definitionGenerator.openAPI.components.schemas.main).to.have.property('properties')
934+
expect(definitionGenerator.openAPI.components.schemas.main.properties).to.have.property('name')
935+
expect(definitionGenerator.openAPI.components.schemas.main.properties).to.have.property('age')
893936
expect(definitionGenerator.openAPI.components.schemas.main).to.not.have.property('$schema')
894937
expect(definitionGenerator.openAPI.components.schemas.main).to.not.have.property('$definitions')
895938
expect(expected).to.equal('#/components/schemas/main')
@@ -1052,9 +1095,8 @@ describe('DefinitionGenerator', () => {
10521095
})
10531096

10541097
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')
1098+
expect(definitionGenerator.openAPI.components.schemas.LicensedMember).to.have.property('type')
1099+
expect(definitionGenerator.openAPI.components.schemas.LicensedMember.type).to.be.equal('string')
10581100
expect(definitionGenerator.openAPI.components.schemas.LicensedMember).to.not.have.property('$schema')
10591101
expect(definitionGenerator.openAPI.components.schemas.LicensedMember).to.not.have.property('$definitions')
10601102
expect(expected).to.equal('#/components/schemas/LicensedMember')

0 commit comments

Comments
 (0)