Skip to content

Commit 9bad1be

Browse files
authored
Merge pull request #5 from JaredCE/properly-clean-schema-references
Properly clean schema references
2 parents e7d9d99 + 62d0c24 commit 9bad1be

File tree

4 files changed

+84
-18
lines changed

4 files changed

+84
-18
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,37 @@ const ConvertorFactory = require('index')
8181
8282
const convertedSchema = ConvertorFactory.convert(jsonSchema)
8383
```
84+
85+
If you already have a referenced schema named "main", it will append a uuid to "main" like thus:
86+
87+
```
88+
{
89+
schemas: {
90+
main: {
91+
"type": "string"
92+
}
93+
"main-547d55a5-87e9-444e-8783-e64f6863d20e": {
94+
"title": "JSON API Schema",
95+
"description": "This is a schema for responses in the JSON API format. For more, see http://jsonapi.org",
96+
"type": "object",
97+
"required": [
98+
"errors"
99+
],
100+
"properties": {
101+
"errors": {
102+
"type": "object",
103+
"properties": {
104+
"message": {
105+
"allOf": [
106+
{
107+
"$ref": "#/components/schemas/main"
108+
}
109+
]
110+
}
111+
}
112+
}
113+
}
114+
}
115+
}
116+
}
117+
```

package-lock.json

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

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
"name": "json-schema-for-openapi",
33
"version": "0.1.0",
44
"description": "Converts a regular JSON Schema to a compatible OpenAPI 3 Schema Object, extracting out $ref schemas to their own schema object",
5-
"keywords": ["json", "json-schema", "openAPI", "openAPI-v3"],
5+
"keywords": [
6+
"json",
7+
"json-schema",
8+
"openAPI",
9+
"openAPI-v3"
10+
],
611
"main": "index.js",
712
"scripts": {
813
"test": "mocha --config './test/.mocharc.js'"
@@ -25,6 +30,7 @@
2530
"sinon": "^14.0.0"
2631
},
2732
"dependencies": {
28-
"json-schema-traverse": "^1.0.0"
33+
"json-schema-traverse": "^1.0.0",
34+
"uuid": "^8.3.2"
2935
}
3036
}

src/Convertor.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

3-
const traverse = require('json-schema-traverse');
3+
const traverse = require('json-schema-traverse')
4+
const {v4: uuid} = require('uuid')
45

56
class Convertor {
67
constructor(schema) {
@@ -78,25 +79,36 @@ class Convertor {
7879

7980
traverse(this.schema, traversal)
8081

82+
for (const [key, value] of Object.entries(this.referencedSchemas)) {
83+
const path = value.split('/').slice(1)
84+
const pathKey = path.pop()
85+
delete path.reduce((previous, current) => previous[current], this.schema)[pathKey]
86+
}
87+
88+
this.removeEmpty(this.schema)
89+
8190
if (Object.keys(this.components).includes('main') === false) {
82-
// for (const [key, value] of Object.entries(this.referencedSchemas)) {
83-
// const path = value.split('/')
84-
// path.shift()
85-
// delete this.schema[path]
86-
// // const objPath = path.join('.')
87-
// // const newField = objPath.split('.').reduce((p,c)=>p&&p[c], this.schema)
88-
// // delete this.schema
89-
// }
90-
// console.log(this.schema)
91-
92-
delete this.schema.definitions
93-
// if (this.schema.$schema) {
94-
// delete this.schema.$schema;
95-
// }
9691
Object.assign(this.components.schemas, {'main': this.schema})
92+
} else {
93+
Object.assign(this.components.schemas, {[`main-${uuid()}`]: this.schema})
9794
}
95+
9896
return this.components
9997
}
98+
99+
removeEmpty(schema) {
100+
Object.keys(schema).forEach(key => {
101+
if (schema[key]
102+
&& typeof schema[key] === 'object'
103+
&& this.removeEmpty(schema[key]) === null) {
104+
delete schema[key]
105+
}
106+
})
107+
108+
if (Object.keys(schema).length === 0) {
109+
return null
110+
}
111+
}
100112
}
101113

102114
module.exports = Convertor

0 commit comments

Comments
 (0)