cebe / php-openapi

Read and write OpenAPI yaml/json files and make the content accessible in PHP objects.
MIT License
466 stars 88 forks source link

Some boolean/array properties have wrong values #77

Closed Neokil closed 2 years ago

Neokil commented 4 years ago

When you are defining a Parameter like this:

"properties": {
    "code": {
        "description": "The Code of the Exception",
        "type": "integer",
        "example": 0
    },
    //...
}

and you try to get settings on that "code"-Property that are not set you will get values instead of null or undefined. For example trying to get the "exclusiveMinimum"-Property will return "false" instead of null/undefined. This causes other frameworks like "justinrainbow/json-schema" to be unable to handle this. I found the code responsible for this behaviour in src/SpecBaseObject.php::__get (Line 341):

337    if (isset($this->attributes()[$name])) {
338        if (is_array($this->attributes()[$name])) {
339            return [];
340        } elseif ($this->attributes()[$name] === Type::BOOLEAN) {
341            return false;
342        }
343        return null;
344    }

Is there a reason why array and boolean are specially handled here? From my POV that is strange because when I am trying to get the value of a setting that is not set I would expect those to be undefined. Best example is the "exclusiveMinimum"-Setting of the Integer-Type which will be set to false by this function if not set what will cause the schema to be invalid because there is no minimum defined.

cebe commented 3 years ago

Good questions! The code in SpecBaseObject is quite generic and not related to Schema definitions directly. The idea was that if we know the type of a property to return a value that fits in that type. That means empty array for array type and false for a boolean that is not explicitily set. There are some values that have a default value of true, e.g.

https://github.com/cebe/php-openapi/blob/cc6915b757a30c94965dce4857e985d0b45d1fd8/src/spec/Schema.php#L121

which corresponds to the spec:

http://spec.openapis.org/oas/v3.0.3#properties additionalProperties - Value can be boolean or object. Inline or referenced schema MUST be of a Schema Object and not a standard JSON Schema. Consistent with JSON Schema, additionalProperties defaults to true.

For the cases you name, we need to check if they need to be adjusted.

cebe commented 2 years ago

fixed. thanks for reporting!