Skip to content

Commit f0da00e

Browse files
committed
convert camel cased keys
1 parent e223d9c commit f0da00e

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

src/Convertor.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,26 @@ class Convertor {
77
constructor(schema = {}) {
88
this.schema = JSON.parse(JSON.stringify(schema))
99

10+
this.camelCasedProperties = [
11+
'allOf',
12+
'oneOf',
13+
'anyOf',
14+
'additionalProperties',
15+
'multipleOf',
16+
'exclusiveMaximum',
17+
'exclusiveMinimum',
18+
'maxLength',
19+
'minLength',
20+
'maxItems',
21+
'minItems',
22+
'uniqueItems',
23+
'maxProperties',
24+
'minProperties',
25+
'readOnly',
26+
'writeOnly',
27+
'externalDocs',
28+
]
29+
1030
this.specialSchemaFields = [
1131
'type',
1232
'allOf',
@@ -85,6 +105,7 @@ class Convertor {
85105
this.convertArrays(schema)
86106
this.convertIfThenElse(schema)
87107
this.convertTypeArrays(schema)
108+
this.dealWithCamelCase(schema)
88109
this.convertDependencies(schema)
89110
this.removeEmptyRequired(schema)
90111
this.convertNullProperty(schema)
@@ -314,6 +335,21 @@ class Convertor {
314335
delete schema.else
315336
}
316337

338+
dealWithCamelCase(schema) {
339+
for (const key of Object.keys(schema)) {
340+
const camelCasedKey = this.camelCasedProperties.filter(camelCasedKey => {
341+
if (key.toLowerCase() === camelCasedKey.toLowerCase()) {
342+
return camelCasedKey
343+
}
344+
})
345+
346+
if (camelCasedKey.length && camelCasedKey[0] !== key) {
347+
schema[camelCasedKey[0]] = schema[key]
348+
delete schema[key]
349+
}
350+
}
351+
}
352+
317353
convertDependencies(schema) {
318354
if (schema.dependencies || schema.dependentSchemas || schema.dependentRequired) {
319355
const allOf = []
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"title": "JSON API Schema",
4+
"description": "This is a schema for responses in the JSON API format. For more, see http://jsonapi.org",
5+
"type": "object",
6+
"properties": {
7+
"name": {
8+
"oneof": [
9+
{
10+
"type": "string"
11+
},
12+
{
13+
"type": "number"
14+
}
15+
]
16+
}
17+
}
18+
}

test/src/Convertor.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ const dependenciesArray = require('../schemas/dependencies/dependenciesArray.jso
4444
const dependenciesSchema = require('../schemas/dependencies/dependenciesSchema.json')
4545
const dependentRequired = require('../schemas/dependencies/dependentRequired.json')
4646
const dependentSchemas = require('../schemas/dependencies/dependentSchemas.json')
47+
// camelcased keys
48+
const camelCased = require('../schemas/camelCasedKey/camelCasedKey.json')
4749
// External Schemas That I Cannot Currently Convert
4850
const listOfBannedSchemas = require('../schemas/SchemasThatCannotBeConverted/list.json')
4951

@@ -566,6 +568,23 @@ describe('Convertor', () => {
566568
});
567569
});
568570

571+
describe('camelCased Keys', () => {
572+
it('should convert camelCasedKey correctly', async function() {
573+
const newConvertor = new Convertor(camelCased)
574+
const result = newConvertor.convert('basic')
575+
expect(result.schemas.basic.properties.name).to.not.have.property('oneof')
576+
expect(result.schemas.basic.properties.name).to.have.property('oneOf')
577+
578+
const cloned = JSON.parse(JSON.stringify(basicOpenAPI))
579+
Object.assign(cloned, {components: result})
580+
expect(cloned).to.have.property('components')
581+
expect(cloned.components).to.have.property('schemas')
582+
expect(cloned.components.schemas).to.have.property('basic')
583+
let valid = await validator.validateInner(cloned, {})
584+
expect(valid).to.be.true
585+
});
586+
});
587+
569588
xdescribe('use a repo with lots of schemas to find failing ones', () => {
570589
it('should convert all schemas successfully', async function() {
571590
this.timeout(1000000);

0 commit comments

Comments
 (0)