OpenAPI-Specification icon indicating copy to clipboard operation
OpenAPI-Specification copied to clipboard

How to support expanded sub-resources?

Open davidsklar opened this issue 11 years ago • 5 comments

I suspect the answer to this is going to be "Swagger targets deterministic APIs and this is not deterministic" but I figured I'd ask anyway.

I am working on an API where many responses look like this:

{ 'self': 'http://link/to/this/resource,
   'name': 'alice',
   'aroma': 'delightful',
   'breakfast': 'http://link/to/breakfast/resource,
   'lunch': 'http://link/to/lunch/resource,
   'dinner': 'http://link/to/dinner/resource
}

As a convenience/request optimization, the API wants to support a query string parameter like expand=lunch or expand=lunch,dinner which would make the values of the lunch (or lunch and dinner) keys hashes (what you would get by retrieving the resource URL) instead of URLs. E.g.

{ 'self': 'http://link/to/this/resource,
   'name': 'alice',
   'aroma': 'delightful',
   'breakfast': 'http://link/to/breakfast/resource,
   'lunch': {
      'self': 'http://link/to/lunch/resource,
      'meal': 'sandwich',
      'drink': 'soda pop'
   }
   'dinner': 'http://link/to/dinner/resource
}

Is expressing this doable with Swagger? If not, I suppose the alternative is to leave the references to links alone and have a separate 'included' property of the top-level resource with optional sub-resources that are included (ala JSON-API).

davidsklar avatar Mar 27 '15 13:03 davidsklar

Expressing it is actually doable, but to an extent.

What you can do:

  • Express a query parameter that accepts one or more values (constrained by an enum if you wish).
  • Express several formats of the same core object using inheritance/polymorphism. This would require you to create a complex scheme, which would be more complex the more strings you'd want to expand - but it is doable.

What you can't do:

  • Express the relation between how the value of the query parameter affects which model is actually going to be used.

There are related to issues to what you raise here, and it would be great if you would join in and voice your opinion on them. While we don't make any promises, the voice of the community impacts the specification quite a bit, as it had for Swagger 2.0.

webron avatar Mar 27 '15 14:03 webron

Got it, thanks for the quick reply.

So for the "what I can do" -- the query parameter expression is pretty straightforward, I get that. For the separate forms of the core object, I don't think I understand completely. I can do this:

definitions:
  BaseThing:
    properties:
      self:
        type: string
      breakfast:
        type: string
        format: url
      dinner:
        type: string
        format: url
  UnexpandedThing:
    allOf:
      - $ref: '#/definitions/BaseThing'
      - properties:
        lunch:
          type: string
  ExpandedThing:
    allOf:
      - $ref: '#/definitions/BaseThing'
      - properties:
        lunch:
            $ref: '#/definitions/Lunch'
  Lunch:
    properties:
      self:
        type: string
      meal:
        type: string
      drink:
        type: string

But then do I still need a discriminator in BaseThing (that would have a value of either UnexpandedThing or ExpandedThing)?

And then what would I reference in my response to indicate that the response could be either expanded or unexpanded? e.g.

paths:
  /something:
    get:
      responses:
        200:
          schema:
            $ref: '#/definitions/SomethingThatEncapsulatesBothThings?'

In terms of voicing my opinion, does that discussion happen in github issues? Is there a mailing list?

davidsklar avatar Mar 27 '15 14:03 davidsklar

Right, the discriminator may be the catch in your use case as it has to be a specific property in your model.

In the response, you'd refer to the BaseThing, again, assuming there's a discriminator.

As for the discussions, they happen here in this repository on issues that are labeled Swagger.Next (note, it doesn't matter if they are open or close).

webron avatar Mar 27 '15 14:03 webron

Excellent, thanks!

davidsklar avatar Mar 27 '15 14:03 davidsklar

Expressing it is actually doable, but to an extent.

What you can do:

* Express a query parameter that accepts one or more values (constrained by an enum if you wish).

* Express several formats of the same core object using inheritance/polymorphism. This would require you to create a complex scheme, which would be more complex the more strings you'd want to expand - but it is doable.

What you can't do:

* Express the relation between how the value of the query parameter affects which model is actually going to be used.

There are related to issues to what you raise here, and it would be great if you would join in and voice your opinion on them. While we don't make any promises, the voice of the community impacts the specification quite a bit, as it had for Swagger 2.0.

@webron is your statement still up to date or are you aware of changes to the specification that make things easier for this problem?


* Express the relation between how the value of the query parameter affects which model is actually going to be used.

Could this be a use case for an x-property?

Thanks for your time

mrtnbrst avatar Jul 26 '23 21:07 mrtnbrst