swaggest / php-json-schema

High definition PHP structures with JSON-schema based validation
MIT License
451 stars 51 forks source link

applyDefaults doesn't work when the default is null #107

Closed ianling closed 4 years ago

ianling commented 4 years ago

Due to the following line, applyDefaults fails to apply a default if that default is null.

https://github.com/swaggest/php-json-schema/blob/93a7278cf738d2e8084b175c2f5de29b97863a06/src/Schema.php#L799

I'm not sure what a good solution would be here. I have a situation where it would be ideal for me to be able to set the default to null. I can work around it easily, but I would prefer that this situation just worked.

As a side note, this is supported by Opis JSON Schema. It looks like they take an approach where they check if a default was set for this property in the schema before trying to use the default. This repo simply sets the default to null if a default is not present in the schema.

vearutop commented 4 years ago

Thank you for raising this issue, let me check if it is feasible to declare default as a three-state value (absent, null, not-null) in a same way const is declared now.

vearutop commented 4 years ago

Please check if v0.12.31 works for you.