1

This pattern appears repeatedly (six times) in my project:

 type: object properties: total: type: integer description: the count of all items that match the query hits: type: array description: a single page of results items: $ref: '#/definitions/{various schema}' 

The inner part of this repeated pattern ({various schema}) varies on each use. I'd like to reference shared code for each of these rather than repeating myself. I'd usually use $ref, but that doesn't seem to work here because of the variable bit.

I've tried to make anyOf work for me, but it only helps vary properties of an object, but I'm trying to vary items of an array.

Is there something I'm missing? Possibly a minor refactor to make this fit into a reusable pattern?

1 Answer 1

1

You may define your repeated pattern, except the items constraint, and then use allOf in each variation.

Your reusable schema would be this way:

 reusable: type: object properties: total: type: integer description: the count of all items that match the query hits: type: array description: a single page of results 

When you want to define a variation, you make use of allOf adding the reusable schema and the additional constraint:

variation1: allOf: - reusable - properties: hits: items: $ref: '#/definitions/variation_schema' 
Sign up to request clarification or add additional context in comments.

1 Comment

That brings my ten lines down to six, still repeated six times. Not what I had hoped for, but still better than anything I thought of. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.