Skip to content

Commit 3c95ebb

Browse files
authored
Merge pull request #5 from JaredAAT/deal-with-additional-properties
Deal with additional properties
2 parents 784f8f3 + 2f2b31b commit 3c95ebb

File tree

4 files changed

+159
-2
lines changed

4 files changed

+159
-2
lines changed

src/Convertor.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ class Convertor {
77
constructor(schema) {
88
this.schema = JSON.parse(JSON.stringify(schema))
99

10-
// this.specialProperties = ['allOf', 'oneOf', 'anyOf', 'not', 'items', 'properties', 'additionalProperties']
11-
this.specialProperties = ['allOf', 'anyOf', 'items', 'oneOf', 'not', 'properties']
10+
this.specialProperties = ['allOf', 'anyOf', 'items', 'oneOf', 'not', 'properties', 'additionalProperties']
1211
this.ofProperties = ['allOf', 'anyOf', 'oneOf']
1312
this.referencedSchemas = {}
1413
this.bannedKeyWords = ['$schema', '$comment', '$id', 'version', 'examples', 'id']
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
"required": [
7+
"errors"
8+
],
9+
"properties": {
10+
"errors": {
11+
"type": "object"
12+
},
13+
"Logging": {
14+
"$ref": "#/definitions/logging"
15+
}
16+
},
17+
"definitions": {
18+
"logLevel": {
19+
"description": "Log level configurations used when creating logs. Only logs that exceeds its matching log level will be enabled. Each log level configuration has a category specified by its JSON property name. For more information about configuring log levels, see https://docs.microsoft.com/aspnet/core/fundamentals/logging/#configure-logging.",
20+
"type": "object",
21+
"additionalProperties": {
22+
"$ref": "#/definitions/logLevelThreshold"
23+
}
24+
},
25+
"logLevelThreshold": {
26+
"description": "Log level threshold.",
27+
"type": "string",
28+
"enum": [
29+
"Trace",
30+
"Debug",
31+
"Information",
32+
"Warning",
33+
"Error",
34+
"Critical",
35+
"None"
36+
]
37+
},
38+
"logging": {
39+
"type": "object",
40+
"description": "Configuration for Microsoft.Extensions.Logging.",
41+
"properties": {
42+
"LogLevel": {
43+
"$ref": "#/definitions/logLevel"
44+
},
45+
"Console": {
46+
"properties": {
47+
"LogLevel": {
48+
"$ref": "#/definitions/logLevel"
49+
},
50+
"FormatterName": {
51+
"description": "Name of the log message formatter to use. Defaults to 'simple'.",
52+
"type": "string",
53+
"default": "simple"
54+
},
55+
"FormatterOptions": {
56+
"description": "Log message formatter options. Additional properties are available on the options depending on the configured formatter. The formatter is specified by FormatterName.",
57+
"type": "object",
58+
"properties": {
59+
"IncludeScopes": {
60+
"description": "Include scopes when true. Defaults to false.",
61+
"type": "boolean",
62+
"default": false
63+
},
64+
"TimestampFormat": {
65+
"description": "Format string used to format timestamp in logging messages. Defaults to null.",
66+
"type": "string"
67+
},
68+
"UseUtcTimestamp": {
69+
"description": "Indication whether or not UTC timezone should be used to for timestamps in logging messages. Defaults to false.",
70+
"type": "boolean",
71+
"default": false
72+
}
73+
}
74+
},
75+
"LogToStandardErrorThreshold": {
76+
"description": "The minimum level of messages are written to Console.Error.",
77+
"$ref": "#/definitions/logLevelThreshold"
78+
}
79+
}
80+
},
81+
"EventSource": {
82+
"properties": {
83+
"LogLevel": {
84+
"$ref": "#/definitions/logLevel"
85+
}
86+
}
87+
},
88+
"Debug": {
89+
"properties": {
90+
"LogLevel": {
91+
"$ref": "#/definitions/logLevel"
92+
}
93+
}
94+
},
95+
"EventLog": {
96+
"properties": {
97+
"LogLevel": {
98+
"$ref": "#/definitions/logLevel"
99+
}
100+
}
101+
},
102+
"ElmahIo": {
103+
"properties": {
104+
"LogLevel": {
105+
"$ref": "#/definitions/logLevel"
106+
}
107+
}
108+
},
109+
"ElmahIoBreadcrumbs": {
110+
"properties": {
111+
"LogLevel": {
112+
"$ref": "#/definitions/logLevel"
113+
}
114+
}
115+
}
116+
},
117+
"additionalProperties": {
118+
"type": "object",
119+
"description": "Logging configuration for a provider. The provider name must match the configuration's JSON property property name.",
120+
"properties": {
121+
"LogLevel": {
122+
"$ref": "#/definitions/logLevel"
123+
}
124+
}
125+
}
126+
}
127+
}
128+
}

test/schemas/complex-propertyDefinition.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
"type": "string"
2727
}
2828
}
29+
},
30+
"dataType": {
31+
"oneOf": [
32+
{
33+
"$ref": "#/definitions/meta"
34+
}
35+
]
2936
}
3037
}
3138
}

test/src/Convertor.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const complexResolvedDefinitionSchema = require('../schemas/complex-resolvedDefi
2222
const complexNullTypeSchema = require('../schemas/complex-null')
2323
const complexTypeArraySchema = require('../schemas/complex-typeArray')
2424
const complexDefaultValuesSchema = require('../schemas/complex-defaultValues')
25+
const complexAdditionalPropertiesSchema = require('../schemas/complex-additionalProperties')
2526

2627
const simpleOpenAPI = require('../openAPI/simple')
2728

@@ -43,6 +44,7 @@ describe('Convertor', () => {
4344
delete require.cache[require.resolve('../schemas/complex-null')];
4445
delete require.cache[require.resolve('../schemas/complex-typeArray')];
4546
delete require.cache[require.resolve('../schemas/complex-defaultValues')];
47+
delete require.cache[require.resolve('../schemas/complex-additionalProperties')];
4648
convertor = new Convertor(simpleSchema)
4749
});
4850

@@ -441,6 +443,27 @@ describe('Convertor', () => {
441443
});
442444
});
443445

446+
describe('convert a schema with additionalProperties containing refs', () => {
447+
it('should return a schema valid for OpenAPI v3.0.0', async function() {
448+
const complexConvertor = new Convertor(complexAdditionalPropertiesSchema)
449+
const components = complexConvertor.convert()
450+
451+
const cloned = JSON.parse(JSON.stringify(simpleOpenAPI))
452+
let valid = await validator.validateInner(cloned, {})
453+
expect(valid).to.be.true
454+
Object.assign(cloned, {components})
455+
expect(cloned).to.have.property('components')
456+
expect(cloned.components).to.have.property('schemas')
457+
expect(cloned.components.schemas).to.have.property('main')
458+
expect(cloned.components.schemas.main).to.not.have.property('definitions')
459+
valid = await validator.validateInner(cloned, {})
460+
.catch(err => {
461+
console.log(err)
462+
})
463+
expect(valid).to.be.true
464+
});
465+
});
466+
444467
describe('use a repo with lots of schemas to find failing ones', () => {
445468
xit('should convert all schemas successfully', async function() {
446469
this.timeout(5000);

0 commit comments

Comments
 (0)