Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[13.x] Allow JsonSchema fluent boolean flags to be unset#60239

Open
LucasCavalheri wants to merge 1 commit into
laravel:13.xfrom
LucasCavalheri:fix/json-schema-toggle-fluent-flags
Open

[13.x] Allow JsonSchema fluent boolean flags to be unset#60239
LucasCavalheri wants to merge 1 commit into
laravel:13.xfrom
LucasCavalheri:fix/json-schema-toggle-fluent-flags

Conversation

@LucasCavalheri
Copy link
Copy Markdown
Contributor

Summary

This PR fixes the fluent boolean flag API in JsonSchema types so that
previously enabled flags may also be disabled.

Currently, methods such as:

required(bool $required = true)
nullable(bool $nullable = true)
unique(bool $unique = true)

only apply changes when the given value is true.

This works for fresh instances:

JsonSchema::string()->nullable(false)

because nothing is enabled yet.

However, it fails when composing schemas conditionally or attempting to
remove previously applied state:

JsonSchema::string()->nullable()->nullable(false);

JsonSchema::string()->required()->required(false);

JsonSchema::array()->unique()->unique(false);

Before this change, passing false would not unset the existing flag.

Changes

Updated the fluent flag setters to allow disabling previously enabled state.

In src/Illuminate/JsonSchema/Types/Type.php:

$this->required = $required ?: null;

$this->nullable = $nullable ?: null;

In src/Illuminate/JsonSchema/Types/ArrayType.php:

$this->uniqueItems = $unique ?: null;

This keeps the generated schema output clean while allowing conditional
schema composition.

Tests

Added the following tests:

tests/JsonSchema/ArrayTypeTest.php

  • test_it_may_unset_unique_items

tests/JsonSchema/TypeTest.php

  • test_required_may_be_unset
  • test_nullable_may_be_unset

These tests validate both:

  • the serialized schema array output
  • the actual validation behavior against the JSON Schema validator

Files Changed

  • src/Illuminate/JsonSchema/Types/Type.php
  • src/Illuminate/JsonSchema/Types/ArrayType.php
  • tests/JsonSchema/ArrayTypeTest.php
  • tests/JsonSchema/TypeTest.php

Validation

./vendor/bin/phpunit tests/JsonSchema/ArrayTypeTest.php tests/JsonSchema/TypeTest.php

Result:

OK (170 tests, 178 assertions)

Linting was also run on the modified files:

No linter errors found.

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.

1 participant