Skip to content

Commit 882a7ec

Browse files
authored
feat: Added tests for Schema Sibling properties (#30)
1 parent 0a700b1 commit 882a7ec

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { normalize, RefErrorType, RefErrorTypes } from '../../src'
2+
import jsonschemaExtraKeys from '../resources/jsonschema/resolve-ref.jsonschema.json'
3+
import jsonschemaExtraKeys31 from '../resources/jsonschema/resolve-ref.jsonschema31.json'
4+
import { JsonPath } from '@netcracker/qubership-apihub-json-crawl'
5+
6+
describe('Schema References', () => {
7+
describe('OAS 3.x.', () => {
8+
it('Other properties in a "$ref" object MUST be ignored', (done) => {
9+
const onRefResolveError = (message: string, path: JsonPath, ref: string, errorType: RefErrorType) => {
10+
11+
expect(ref).toBe('#/components/schemas/User')
12+
expect(errorType).toBe(RefErrorTypes.RICH_REF_NOT_ALLOWED)
13+
done()
14+
}
15+
16+
const expectedSchema = {
17+
'type': 'object',
18+
'properties': {
19+
'id': {
20+
'type': 'string',
21+
},
22+
},
23+
}
24+
25+
const result = normalize(jsonschemaExtraKeys, { onRefResolveError }) as any
26+
expect(result).toHaveProperty(['paths', '/test', 'get', 'responses', '200', 'content', 'application/json', 'schema'], expectedSchema)
27+
})
28+
})
29+
30+
describe('OAS 3.1.', () => {
31+
it('should support keywords alongside of "$ref" in the same schema object', () => {
32+
const expectedSchema = {
33+
'type': 'object',
34+
'properties': {
35+
'id': {
36+
'type': 'string',
37+
},
38+
},
39+
'description': 'extra description',
40+
}
41+
42+
const result = normalize(jsonschemaExtraKeys31) as any
43+
expect(result).toHaveProperty(['paths', '/test', 'get', 'responses', '200', 'content', 'application/json', 'schema'], expectedSchema)
44+
})
45+
})
46+
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"openapi": "3.0.4",
3+
"info": {
4+
"title": "Test API",
5+
"version": "1.0.0"
6+
},
7+
"paths": {
8+
"/test": {
9+
"get": {
10+
"responses": {
11+
"200": {
12+
"description": "ok",
13+
"content": {
14+
"application/json": {
15+
"schema": {
16+
"$ref": "#/components/schemas/User",
17+
"description": "extra description SHOULD BE DROPPED"
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}
25+
},
26+
"components": {
27+
"schemas": {
28+
"User": {
29+
"type": "object",
30+
"properties": {
31+
"id": {
32+
"type": "string"
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"title": "Test API",
5+
"version": "1.0.0"
6+
},
7+
"paths": {
8+
"/test": {
9+
"get": {
10+
"responses": {
11+
"200": {
12+
"description": "ok",
13+
"content": {
14+
"application/json": {
15+
"schema": {
16+
"$ref": "#/components/schemas/User",
17+
"description": "extra description"
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}
25+
},
26+
"components": {
27+
"schemas": {
28+
"User": {
29+
"type": "object",
30+
"properties": {
31+
"id": {
32+
"type": "string"
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)