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

Doesn't pass nullable for references #50

Closed liquorvicar closed 4 years ago

liquorvicar commented 4 years ago

As far as I can see, it is valid to set a property $ref to be nullable. But I don't think this library handles that.

e.g.

{
  "type": "object",
  "required": [
    "name"
  ],
  "properties": {
    "name": {
      "type": "string"
    },
    "address": {
      "nullable": "true",
      "$ref": "#/components/schemas/Address"
    },
    "age": {
      "type": "integer",
      "format": "int32",
      "minimum": 0
    }
  }
}

I would expect the address property to be nullable when this schema is parsed/loaded.

andy-educake commented 4 years ago

Hmmm, on further inspection it looks like what I'm trying above may not fit the intention for the nullable key word (even if it does technically adhere to the spec).

There is a proposal to clarify what nullable means: https://github.com/OAI/OpenAPI-Specification/blob/master/proposals/003_Clarify-Nullable.md

andy-educake commented 4 years ago

I have found a workaround for now which is to use:



    "address": {
      "nullable": "true",
      "anyof": [
          "$ref": "#/components/schemas/Address"
       ]
    },
cebe commented 4 years ago

a reference object can only contain the $ref key, if it has more than $ref it is undefined how to resolve the reference. If we want to replace the reference object with the object it points to, and that one defines nullable differently from the local object that would be a conflict and the result is undefined. If you need this, the solution you found is the correct way to do it as far as I see.