Skip to content

Commit

Permalink
Remove only discriminator.mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
satazor authored and andreffvalente committed Oct 24, 2024
1 parent 43a1e2c commit 5054d50
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,22 @@ This plugin configures Fastify to coerce `parameters` to the correct type based

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.

#### Using `discriminator`

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:

```js
import Fastify from 'fastify'

const fastify = Fastify({
ajv: {
customOptions: {
discriminator: true
}
}
});
```

## License

[MIT](./LICENSE)
Expand Down
2 changes: 1 addition & 1 deletion src/parser/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const parseBody = (route, operation) => {
addPropertyToSchema(route.schema.headers, { 'content-type': { const: contentType } }, true);

// Sanitize schema.
removeAttributesFromSchema(schema, ['xml', 'example', 'discriminator']);
removeAttributesFromSchema(schema, ['xml', 'example', 'discriminator.mapping']);

// Add request body schema.
route.schema.body = schema;
Expand Down
19 changes: 16 additions & 3 deletions src/parser/body.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,18 @@ describe('parseBody()', () => {

it('should sanitize body schema', () => {
const schema = {
bar: { discriminator: { propertyName: 'type' }, type: 'object' },
foo: { example: 'baz', type: 'string', xml: { name: 'foo' } },
bar: {
discriminator: {
mapping: { bar: 'baz', foo: 'foz' },
propertyName: 'type'
},
type: 'object'
},
foo: {
example: 'baz',
type: 'string',
xml: { name: 'foo' }
},
required: ['foo'],
xml: { name: 'Bar' }
};
Expand All @@ -85,7 +95,10 @@ describe('parseBody()', () => {
parseBody(route, { requestBody });

expect(route.schema.body).toStrictEqual({
bar: { type: 'object' },
bar: {
discriminator: { propertyName: 'type' },
type: 'object'
},
foo: { type: 'string' },
required: ['foo']
});
Expand Down
10 changes: 7 additions & 3 deletions src/utils/schema.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { unset } from 'lodash-es';

export const addPropertyToSchema = (schema, properties, required) => {
for (const key in properties) {
const property = properties[key];
Expand All @@ -19,10 +21,12 @@ export const removeAttributesFromSchema = (schema, attributes) => {
return;
}

for (const attribute of attributes) {
unset(schema, attribute);
}

for (const prop in schema) {
if (attributes.includes(prop)) {
delete schema[prop];
} else if (typeof schema[prop] === 'object') {
if (typeof schema[prop] === 'object') {
removeAttributesFromSchema(schema[prop], attributes);
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/utils/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ describe('removeAttributesFromSchema()', () => {
}
},
foo: {
discriminator: {
mapping: {
bar: 'baz',
foo: 'foz'
},
propertyName: 'type'
},
example: 'approved',
type: 'string'
}
Expand All @@ -115,7 +122,7 @@ describe('removeAttributesFromSchema()', () => {
xml: { name: 'xml-schema' }
};

removeAttributesFromSchema(schema, ['xml', 'example']);
removeAttributesFromSchema(schema, ['xml', 'example', 'discriminator.mapping']);

expect(schema).toStrictEqual({
properties: {
Expand All @@ -126,6 +133,7 @@ describe('removeAttributesFromSchema()', () => {
type: 'string'
},
foo: {
discriminator: { propertyName: 'type' },
type: 'string'
}
},
Expand Down

0 comments on commit 5054d50

Please sign in to comment.