swagger-php icon indicating copy to clipboard operation
swagger-php copied to clipboard

readOnly for property of type object

Open willylambert opened this issue 10 months ago • 3 comments

Hello,

The following code

   #[OA\Property(readOnly:true)]
  public Article $article;

  #[OA\Property(readOnly:true)]
  public string $label;

Produce readonly property only for string property in json openapi, not for object :

"article": {
    "$ref": "#/components/schemas/Article"
},
"label": {
    "type": "string",
    "readOnly": true
},

Is it the expected behavior ?

willylambert avatar Apr 11 '24 09:04 willylambert

Curretly yes. The spec for 3.0 doesn't allow any other properties next to $ref and 3.1 only allows title/description. The solution would be to express $ref's with additional properties using a different JSON contruct altogether. Actually, that is something I've started thinking about lately as there seem to be more and more of those transformations recently. In fact, that might also help with a lot of the 3.0/3.1 conditional code.

For now this is, unfotunately, the expected behaviour.

DerManoMann avatar Apr 14 '24 01:04 DerManoMann

Thanks for your reply,

I've used the allOf attribute to set property as readonly, and it worked.

  #[OA\Property(allOf: [new OA\Schema(ref:"#/components/schemas/Article")],readOnly:true)]
  public $article;

=>

"article": {
  "readOnly": true,
  "allOf": [
    {
      "$ref": "#/components/schemas/Article"
    }
  ]
},

willylambert avatar Apr 16 '24 09:04 willylambert

Cool. I suppose this could be a pattern to support any property in combination with $ref, although I am not sure how valid that would be...

DerManoMann avatar Apr 17 '24 04:04 DerManoMann