Skip to content

Conversation

cesco69
Copy link
Contributor

@cesco69 cesco69 commented Oct 8, 2025

Just a small performance improvements when an object contains only one key or an array only one item

Side note: the benchmark don't cover this case

Just a small performance improvements when an array has only one item

Signed-off-by: francesco <[email protected]>
@cesco69 cesco69 marked this pull request as ready for review October 8, 2025 10:40
@cesco69 cesco69 changed the title perf: avoid addComma on empty array perf: avoid addComma when not necessary Oct 8, 2025
Copy link
Contributor

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach is good, but additionalProperties can break your neck

@Uzlopak
Copy link
Contributor

Uzlopak commented Oct 8, 2025

https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-00#rfc.section.10.3.2.3

10.3.2.3. additionalProperties
The value of "additionalProperties" MUST be a valid JSON Schema.

The behavior of this keyword depends on the presence and annotation results of "properties" and "patternProperties" within the same schema object. Validation with "additionalProperties" applies only to the child values of instance names that do not appear in the annotation results of either "properties" or "patternProperties".

For all such properties, validation succeeds if the child instance validates against the "additionalProperties" schema.

The annotation result of this keyword is the set of instance property names validated by this keyword's subschema.

Omitting this keyword has the same assertion behavior as an empty schema.

Implementations MAY choose to implement or optimize this keyword in another way that produces the same effect, such as by directly checking the names in "properties" and the patterns in "patternProperties" against the instance property set. Implementations that do not support annotation collection MUST do so.

And what does empty schema do?

https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-00#rfc.section.4.3.2

The boolean schema values "true" and "false" are trivial schemas that always produce themselves as assertion results, regardless of the instance value. They never produce annotation results.

These boolean schemas exist to clarify schema author intent and facilitate schema processing optimizations. They behave identically to the following schema objects (where "not" is part of the subschema application vocabulary defined in this document).

true:
Always passes validation, as if the empty schema {}
false:
Always fails validation, as if the schema { "not": {} }
While the empty schema object is unambiguous, there are many possible equivalents to the "false" schema. Using the boolean values ensures that the intent is clear to both human readers and implementations.

In result, this means, that if additionalProperties is not set additionalProperties: undefined, it is equivalent to empty schema additionalProperties: {}, which is equivalent to setting it to true additionalProperties: true.

So, to be compliant with json schema, you have to check that it is explicitly not false.

Signed-off-by: francesco <[email protected]>
@cesco69

This comment was marked as outdated.

@cesco69
Copy link
Contributor Author

cesco69 commented Oct 8, 2025

done a1fade3

@cesco69 cesco69 requested a review from Uzlopak October 8, 2025 14:52
@Uzlopak
Copy link
Contributor

Uzlopak commented Oct 8, 2025

Can you add some tests please?

Signed-off-by: francesco <[email protected]>
@cesco69
Copy link
Contributor Author

cesco69 commented Oct 8, 2025

done c8a0a47

Side note: the current tests also cover this changes

@Uzlopak
Copy link
Contributor

Uzlopak commented Oct 8, 2025

Side note: the current tests also cover this changes

Why did the CI not fail on first commit?

@cesco69
Copy link
Contributor Author

cesco69 commented Oct 9, 2025

Why did the CI not fail on first commit?

also my new test c8a0a47 don't fail with first commit 😂

I didn't understand what I have to stay in this PR. It seems to me that the existing tests already cover all PR cases

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

Successfully merging this pull request may close these issues.

3 participants