Skip to content

Commit 2a258bd

Browse files
Alan-ChaErikWittern
authored andcommitted
Improve tests for handling additionalProperties
Signed-off-by: Alan Cha <[email protected]>
1 parent 0ecb7f7 commit 2a258bd

File tree

3 files changed

+87
-25
lines changed

3 files changed

+87
-25
lines changed

packages/openapi-to-graphql/test/example_api.test.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -801,24 +801,37 @@ test('Request data is correctly de-sanitized to be sent', () => {
801801
})
802802
})
803803

804+
// Testing additionalProperties field in schemas
804805
test('Fields with arbitrary JSON (e.g., maps) can be returned', () => {
805806
const query = `{
806-
car (username: "arlene") {
807-
color
808-
model
807+
cars {
809808
tags
810809
}
811810
}`
812811
return graphql(createdSchema, query, null, {}).then(result => {
813812
expect(result).toEqual({
814813
data: {
815-
car: {
816-
color: 'black',
817-
model: 'BMW 7 series',
818-
tags: {
819-
impression: 'decadent'
814+
"cars": [
815+
{
816+
"tags": null
817+
},
818+
{
819+
"tags": {
820+
"speed": "extreme"
821+
}
822+
},
823+
{
824+
"tags": {
825+
"impression": "decadent",
826+
"condition": "slightly beat-up"
827+
}
828+
},
829+
{
830+
"tags": {
831+
"impression": "decadent"
832+
}
820833
}
821-
}
834+
]
822835
}
823836
})
824837
})
@@ -834,7 +847,7 @@ test('Capitalized enum values can be returned', () => {
834847
expect(result).toEqual({
835848
data: {
836849
car: {
837-
kind: 'LIMOSINE'
850+
kind: 'SEDAN'
838851
}
839852
}
840853
})

packages/openapi-to-graphql/test/example_api_server.js

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,39 @@ function startServer(PORT) {
9393
}
9494
}
9595

96+
const Cars = {
97+
arlene: {
98+
model: 'Retro Rides',
99+
color: 'yellow',
100+
kind: 'SEDAN'
101+
},
102+
will: {
103+
model: 'Speedzone Speedster',
104+
color: 'red',
105+
tags: {
106+
speed: 'extreme'
107+
},
108+
kind: 'RACE_CAR'
109+
},
110+
johnny: {
111+
model: 'Glossy German',
112+
color: 'silver',
113+
tags: {
114+
impression: 'decadent',
115+
condition: 'slightly beat-up'
116+
},
117+
kind: 'LIMOSINE'
118+
},
119+
heather: {
120+
model: 'Glossy German',
121+
color: 'black',
122+
tags: {
123+
impression: 'decadent'
124+
},
125+
kind: 'LIMOSINE'
126+
}
127+
}
128+
96129
const Companies = {
97130
binsol: {
98131
id: 'binsol',
@@ -344,18 +377,8 @@ function startServer(PORT) {
344377

345378
app.get('/api/users/:username/car', (req, res) => {
346379
console.log(req.method, req.path)
347-
if (
348-
typeof req.params.username === 'string' &&
349-
req.params.username in Users
350-
) {
351-
res.send({
352-
model: 'BMW 7 series',
353-
color: 'black',
354-
tags: {
355-
impression: 'decadent'
356-
},
357-
kind: 'LIMOSINE'
358-
})
380+
if (req.params.username in Users) {
381+
res.send(Cars[req.params.username])
359382
} else {
360383
res.status(404).send({
361384
message: 'Wrong username.'
@@ -399,6 +422,11 @@ function startServer(PORT) {
399422
}
400423
})
401424

425+
app.get('/api/cars', (req, res) => {
426+
console.log(req.method, req.path)
427+
res.send(Array.from(Object.values(Cars)))
428+
})
429+
402430
app.get('/api/companies/:id', (req, res) => {
403431
console.log(req.method, req.path)
404432
res.send(Companies[req.params.id])

packages/openapi-to-graphql/test/fixtures/example_oas.json

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,27 @@
199199
}
200200
}
201201
},
202+
"/cars": {
203+
"get": {
204+
"operationId": "getAllCars",
205+
"description": "Returns information about all employee cars",
206+
"responses": {
207+
"200": {
208+
"description": "Everyone's cars",
209+
"content": {
210+
"application/json": {
211+
"schema": {
212+
"type": "array",
213+
"items": {
214+
"$ref": "#/components/schemas/car"
215+
}
216+
}
217+
}
218+
}
219+
}
220+
}
221+
}
222+
},
202223
"/companies/{id}": {
203224
"get": {
204225
"operationId": "getCompanyById",
@@ -893,7 +914,7 @@
893914
],
894915
"responses": {
895916
"200": {
896-
"description": "A user represents a natural person.",
917+
"description": "A trashcan",
897918
"content": {
898919
"application/json": {
899920
"schema": {
@@ -911,7 +932,7 @@
911932
"description": "Returns the (contents of a) trashcan from a specific office",
912933
"responses": {
913934
"200": {
914-
"description": "A user represents a natural person.",
935+
"description": "Many trashcans",
915936
"content": {
916937
"application/json": {
917938
"schema": {
@@ -1082,7 +1103,7 @@
10821103
},
10831104
"kind": {
10841105
"type": "string",
1085-
"enum": ["SEDAN", "SUV", "MINIVAN", "LIMOSINE"]
1106+
"enum": ["SEDAN", "SUV", "MINIVAN", "LIMOSINE", "RACE_CAR"]
10861107
}
10871108
}
10881109
},

0 commit comments

Comments
 (0)