@uphold/fastify-openapi-router-plugin 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -0
- package/package.json +1 -1
- package/src/parser/body.js +1 -1
- package/src/parser/body.test.js +16 -3
- package/src/utils/schema.js +7 -3
- package/src/utils/schema.test.js +9 -1
package/README.md
CHANGED
|
@@ -274,6 +274,22 @@ This plugin configures Fastify to coerce `parameters` to the correct type based
|
|
|
274
274
|
|
|
275
275
|
If your API needs improved coercion support, like `object` types or `cookie` parameters, please [fill an issue](https://github.com/uphold/fastify-openapi-router-plugin/issues/new) to discuss the implementation.
|
|
276
276
|
|
|
277
|
+
#### Using `discriminator`
|
|
278
|
+
|
|
279
|
+
This plugin removes `discriminator.mapping` from schemas since `ajv` (fastify's validator) does not support it. However, to use `discriminator` in your OpenAPI schema, you must also enable `discriminator` option during fastify initialization like so:
|
|
280
|
+
|
|
281
|
+
```js
|
|
282
|
+
import Fastify from 'fastify'
|
|
283
|
+
|
|
284
|
+
const fastify = Fastify({
|
|
285
|
+
ajv: {
|
|
286
|
+
customOptions: {
|
|
287
|
+
discriminator: true
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
```
|
|
292
|
+
|
|
277
293
|
## License
|
|
278
294
|
|
|
279
295
|
[MIT](./LICENSE)
|
package/package.json
CHANGED
package/src/parser/body.js
CHANGED
|
@@ -12,7 +12,7 @@ export const parseBody = (route, operation) => {
|
|
|
12
12
|
addPropertyToSchema(route.schema.headers, { 'content-type': { const: contentType } }, true);
|
|
13
13
|
|
|
14
14
|
// Sanitize schema.
|
|
15
|
-
removeAttributesFromSchema(schema, ['xml', 'example', 'discriminator']);
|
|
15
|
+
removeAttributesFromSchema(schema, ['xml', 'example', 'discriminator.mapping']);
|
|
16
16
|
|
|
17
17
|
// Add request body schema.
|
|
18
18
|
route.schema.body = schema;
|
package/src/parser/body.test.js
CHANGED
|
@@ -71,8 +71,18 @@ describe('parseBody()', () => {
|
|
|
71
71
|
|
|
72
72
|
it('should sanitize body schema', () => {
|
|
73
73
|
const schema = {
|
|
74
|
-
bar: {
|
|
75
|
-
|
|
74
|
+
bar: {
|
|
75
|
+
discriminator: {
|
|
76
|
+
mapping: { bar: 'baz', foo: 'foz' },
|
|
77
|
+
propertyName: 'type'
|
|
78
|
+
},
|
|
79
|
+
type: 'object'
|
|
80
|
+
},
|
|
81
|
+
foo: {
|
|
82
|
+
example: 'baz',
|
|
83
|
+
type: 'string',
|
|
84
|
+
xml: { name: 'foo' }
|
|
85
|
+
},
|
|
76
86
|
required: ['foo'],
|
|
77
87
|
xml: { name: 'Bar' }
|
|
78
88
|
};
|
|
@@ -85,7 +95,10 @@ describe('parseBody()', () => {
|
|
|
85
95
|
parseBody(route, { requestBody });
|
|
86
96
|
|
|
87
97
|
expect(route.schema.body).toStrictEqual({
|
|
88
|
-
bar: {
|
|
98
|
+
bar: {
|
|
99
|
+
discriminator: { propertyName: 'type' },
|
|
100
|
+
type: 'object'
|
|
101
|
+
},
|
|
89
102
|
foo: { type: 'string' },
|
|
90
103
|
required: ['foo']
|
|
91
104
|
});
|
package/src/utils/schema.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { unset } from 'lodash-es';
|
|
2
|
+
|
|
1
3
|
export const addPropertyToSchema = (schema, properties, required) => {
|
|
2
4
|
for (const key in properties) {
|
|
3
5
|
const property = properties[key];
|
|
@@ -19,10 +21,12 @@ export const removeAttributesFromSchema = (schema, attributes) => {
|
|
|
19
21
|
return;
|
|
20
22
|
}
|
|
21
23
|
|
|
24
|
+
for (const attribute of attributes) {
|
|
25
|
+
unset(schema, attribute);
|
|
26
|
+
}
|
|
27
|
+
|
|
22
28
|
for (const prop in schema) {
|
|
23
|
-
if (
|
|
24
|
-
delete schema[prop];
|
|
25
|
-
} else if (typeof schema[prop] === 'object') {
|
|
29
|
+
if (typeof schema[prop] === 'object') {
|
|
26
30
|
removeAttributesFromSchema(schema[prop], attributes);
|
|
27
31
|
}
|
|
28
32
|
}
|
package/src/utils/schema.test.js
CHANGED
|
@@ -107,6 +107,13 @@ describe('removeAttributesFromSchema()', () => {
|
|
|
107
107
|
}
|
|
108
108
|
},
|
|
109
109
|
foo: {
|
|
110
|
+
discriminator: {
|
|
111
|
+
mapping: {
|
|
112
|
+
bar: 'baz',
|
|
113
|
+
foo: 'foz'
|
|
114
|
+
},
|
|
115
|
+
propertyName: 'type'
|
|
116
|
+
},
|
|
110
117
|
example: 'approved',
|
|
111
118
|
type: 'string'
|
|
112
119
|
}
|
|
@@ -115,7 +122,7 @@ describe('removeAttributesFromSchema()', () => {
|
|
|
115
122
|
xml: { name: 'xml-schema' }
|
|
116
123
|
};
|
|
117
124
|
|
|
118
|
-
removeAttributesFromSchema(schema, ['xml', 'example']);
|
|
125
|
+
removeAttributesFromSchema(schema, ['xml', 'example', 'discriminator.mapping']);
|
|
119
126
|
|
|
120
127
|
expect(schema).toStrictEqual({
|
|
121
128
|
properties: {
|
|
@@ -126,6 +133,7 @@ describe('removeAttributesFromSchema()', () => {
|
|
|
126
133
|
type: 'string'
|
|
127
134
|
},
|
|
128
135
|
foo: {
|
|
136
|
+
discriminator: { propertyName: 'type' },
|
|
129
137
|
type: 'string'
|
|
130
138
|
}
|
|
131
139
|
},
|