fastify / fastify/fast-json-stringify

allOf Schema and additionalProperties result in unexpected result

Open
#684 10 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
3.7k
Forks
226
Avg merge
1d 59m
Merged PRs (30d)
3

Description

### Prerequisites

- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the regression has not already been reported

### Last working version

5.11.1

### Stopped working in version

5.12.0

### Node.js version

21.x

### Operating system

macOS

### Operating system version (i.e. 20.04, 11.3, 10)

14.2

### 💥 Regression Report

allOf schema stringify doesn't work as expected. See details below.

### Steps to Reproduce

Here is example:

```js
import FJS from 'fast-json-stringify';
import util from 'util';

const AJV_CONFIG = { removeAdditional: 'all', allErrors: true, coerceTypes: true, strictTypes: false };

const schema = {
allOf: [
{
type: 'object',
properties: {
id: {
type: 'integer'
},
parent_id: {
type: ['null', 'integer'],
default: null
},
name: {
type: 'string'
}
},
required: ['id', 'name']
},
{
type: 'object',
properties: {
integrations: {
type: 'array',
items: {
type: 'object',
properties: {
id: {
type: 'integer'
},
domain: {
type: 'string'
},
is_enabled: {
type: 'boolean'
}
},
required: ['id', 'domain', 'is_enabled']
}
}
},
required: ['integrations']
},
{
type: 'object',
properties: {
parent: {
oneOf: [
{
type: 'object',
properties: {
id: {
type: 'integer'
},
name: {
type: 'string'
}
},
required: ['id', 'name']
},
{
type: 'null'
}
],
default: null
}
}
}
]
};

const s = FJS({ additionalProperties: false, ...schema }, { ajv: AJV_CONFIG });

const input = {
id: 1,
name: 'Name',
integrations: [
{
id: 1,
domain: 'example.com',
is_enabled: 1
}
],
parent_id: 1,
parent: {
id: 1,
name: 'Name'
}
};

console.log(util.inspect(JSON.parse(s(input)), false, null, true));
```

### Expected Behavior

### Version 5.11.1

Expected result:

```js
{
id: 1,
parent_id: 1,
name: 'Name',
integrations: [{ id: 1, domain: 'example.com', is_enabled: true }],
parent: { id: 1, name: 'Name' }
}
```

Next, if i comment down `integrations.domain` inside input object i should receive error:

```
Error: "domain" is required!
```

### Version 5.12.0
Next in version 5.12.0 result will be following:

```
{
id: 1,
name: 'Name',
integrations: [ { id: 1, domain: 'example.com', is_enabled: 1 } ],
parent_id: 1,
parent: { id: 1, name: 'Name' }
}
```

We can see that `integrations.is_enabled` has wrong type.

Next, if i comment down `integrations.domain` inside input object i should receive error, but got this:

```
{
id: 1,
name: 'Name',
integrations: [ { id: 1, is_enabled: 1 } ],
parent_id: 1,
parent: { id: 1, name: 'Name' }
}
```

No error fired and domain property is missed. Remove additional props is not working as well.

Thank you for attention.
Cheers.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.