Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing fields in anyOf subschemas are not ignored as with standard subschemas #736

Closed
2 tasks done
maRci002 opened this issue Sep 5, 2024 · 3 comments
Closed
2 tasks done

Comments

@maRci002
Copy link

maRci002 commented Sep 5, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.28.1

Plugin version

No response

Node.js version

20.17.0

Operating system

Windows

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

22h2

Description

When I add additional properties to a subschema in anyOf, I receive the following error:

TypeError: The value of '#/properties/snippet' does not match schema definition.

Code to reproduce:

import fastJson from 'fast-json-stringify';

const profileObject = {
    snippet: {
        username: '',
        additionalProperty: '',
    },
};

const snippetSchema = {
    type: 'object',
    properties: {
        username: { type: 'string' }
    },
    required: ['username'],
    additionalProperties: false
};

const profileSchemaOptional = {
    type: 'object' as const,
    properties: {
        snippet: snippetSchema
    },
    additionalProperties: false,
    $schema: 'http://json-schema.org/draft-07/schema#'
};

const optionalStringify = fastJson(profileSchemaOptional);
console.log(optionalStringify(profileObject)); // ok

const profileSchemaNullable = {
    type: 'object' as const,
    properties: {
        snippet: {
            anyOf: [
                snippetSchema,
                { type: 'null' } // This line can be removed, but the error will persist.
            ]
        }
    },
    additionalProperties: false,
    $schema: 'http://json-schema.org/draft-07/schema#'
};

const nullableStringify = fastJson(profileSchemaNullable);
console.log(nullableStringify(profileObject)); // throws error

Note: The optionalStringify(profileObject) call allows additional properties in snippetSchema, whereas nullableStringify(profileObject) does not allow them.

Link to code that reproduces the bug

No response

Expected Behavior

Subschemas within anyOf should be serialized just like normal subschemas.

https://github.com/fastify/fast-json-stringify?tab=readme-ov-file#additional-properties

If additionalProperties is not present or is set to false, every property that is not explicitly listed in the properties and patternProperties objects,will be ignored, as described in Missing fields.
Missing fields are ignored to avoid having to rewrite objects before serializing.

@maRci002
Copy link
Author

maRci002 commented Sep 5, 2024

I have already created an issue in the main repository at fastify/fastify#5647, but since the problem originates from fast-json-stringify, I have also opened an issue here.

@maRci002
Copy link
Author

FYI: When "type": ["string", "null"] is used instead of anyOf, it works well for nullable cases. I've submitted an issue to the zod-to-json-schema repository to support this feature. StefanTerdell/zod-to-json-schema#142

@maRci002
Copy link
Author

Since anyOf, oneOf, and allOf are logical validators in JSON Schema that manage how data matches against multiple criteria defined through sub-schemas, this seems to be the expected behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant