7

Assume I have following schema to reuse later using $ref:

"schemas": {
      "Order": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "format": "int64"
          },
          "petId": {
            "type": "integer",
            "format": "int64"
          }
        }
      }

But I have another schema similar to this:

"schemas": {
      "Order": {
        "type": "object",
        "properties": {
          "id": {
            "type": "integer",
            "format": "int64"
          },
          "itemId": {
            "type": "integer",
            "format": "int64"
          }
        }
      }

Only difference between them is itemId and petId, I want to create only one schema and pass itemId or petId when referencing. How to accomplish this? Are there any alternative solutions?

ata
  • 1,254
  • 1
  • 9
  • 30

1 Answers1

5

There's no way to pass arguments along a schema reference, really.

What we could do in your case is to have a base Order schema with just the common properties, and then separate schemas for petId/itemId Orders that utilize allOf.

Check this answer for another example (or this for a more concrete one!).

Aleksi
  • 4,483
  • 33
  • 45
  • Yeah, it seems you are right. I think this feature should be added. I had the same problem with [json schemas](https://json-schema.org/) – ata Aug 16 '19 at 10:41