j-d-carmichael / boats

Beautiful Open Api Template System
MIT License
57 stars 8 forks source link

model pick #32

Closed j-d-carmichael closed 3 years ago

j-d-carmichael commented 3 years ago

Openapi has a nice feature.. allOf.. but doesn't offer the ability to pick attributes from an existing model.

This allows you to create a set of base attributes to be used as say search results... lighter versions of the models. Then using allOf extend to create a fuller model for a detail view... but then when you need a 3rd version.. the whole thing becomes too complex to easily manage.

A function in boats to pick attribute from the full model to create a new model would be much easier to manage, all attributes held in the the main model, the lighters model cherry picking what they needed:

{{ pickForNewObject('../item/model.yml', ['name', 'description', 'createdAt']) }}

Would result in:

type: object
properties:
  description:
    type: string
  name:
    type: string
  createdAt
    type: string
    format: date-time

Where the full model might look like:

type: object
properties:
  comments:
    type: array
    items:
      $ref: ./comment/model.yml
  createdAt
    type: string
    format: date-time
  updatedAt
    type: string
    format: date-time
  deletedAt:
    type: string
    format: date-time
  description:
    type: string
  name:
    type: string
  likes:
    type: array
    items:
      $ref: ./like/model.yml

There will however be an issue when a picked attribute contains a $ref

p-mcgowan commented 3 years ago

Yeah I had a similar idea the other day with extends - oa3 has it build it but if you area still on oa2 then it means any base model is just repeated for slight differences

j-d-carmichael commented 3 years ago

oh really? what is the feature called in oa3?

p-mcgowan commented 3 years ago

https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/

also supports oneOf - so data can be something like id: number OR username: string

j-d-carmichael commented 3 years ago

ah yea but this is a different thing; i thought i had missed a trick :)

Here i want to have 1 common model, and have a lighter model simply pick a specific list of attributes from the common model.

With oneOf this is not possible in a straight forward manner that is not overly verbose and afaik is design primarily for saying "this can be this OR this".

p-mcgowan commented 3 years ago

sure, but with inheritence you can do the ssme thing - basemodel = result_of_pick, regular model = basemodel + pick

j-d-carmichael commented 3 years ago

@jonmanga actually built this one already: pickProps.ts

An inverse function would be pretty helpful though