protean icon indicating copy to clipboard operation
protean copied to clipboard

Vary body-example based on req param values (REST representations)

Open memiah-nick opened this issue 10 years ago • 10 comments

Perhaps something like:

:types {
  :OurVar "(foo|bar)"
}
:vars {
  "ourvar" {:type :OurVar}
}
...
    :rsp {
      :200 {
        :body-example {
          "ourvar" {
            "foo" ["foo-template.json"]
            "bar" ["bar-template.json"]
          }
        }
      }
    }

memiah-nick avatar Aug 05 '15 11:08 memiah-nick

Is the purpose for sim behaviour? In which case, sim lib may handle it?

colin-lamed avatar Aug 05 '15 12:08 colin-lamed

By handling the selection in the simlib, we're not coupling to enum types - we could select one on any relevant of criteria. Either we select the response directly by filename, or we will need to add labels to the examples. e.g.

  :body-example {
    {"foo" "foo-template.json"}
    {"bar" "bar-template.json"}
  }

colin-lamed avatar Aug 05 '15 12:08 colin-lamed

The context for this was with the free out of the box sim (ie no sim extension created yet).

rossm-memiah avatar Aug 05 '15 15:08 rossm-memiah

I'm not convinced we should overload the codex with sim behaviour - especially when this is just one of many possible out-of-the-box behaviours. We should try and make writing a sim-lib as easy as possible.

colin-lamed avatar Aug 05 '15 16:08 colin-lamed

Unless you can argue that we're not just discussing sim behaviour, but also the real api behaviour. i.e. we want to document the coupling between inputs and response shapes.

colin-lamed avatar Aug 05 '15 16:08 colin-lamed

You are spot on with that last comment... I was thinking we may want to document the concept of 'representations'. It may as you suggest be more suitable to do this sim extensions, but it should probably flow out into API docs which means the codex will need to have some idea of a link between elements of request shape and sample response.

rossm-memiah avatar Aug 05 '15 21:08 rossm-memiah

I'm thinking scenarios like pass a header in and get a restricted representation back, for mobile consumption for example.

rossm-memiah avatar Aug 05 '15 21:08 rossm-memiah

We probably don't want to over complicate the body-example notation for the majority of cases where this doesn't apply. How about attaching additional/optional influences to body-example selection - which would also allow us to extend if we find other cases. e.g.

:body-example {
   "foo-template.json" {:var "ourvar" :val "foo" :default true}
   "bar-template.json" {:var "ourvar" :val "bar"}
 }

(I added default which may be useful to solve #180).

or maybe something similar notation to query-params, using a vector.

colin-lamed avatar Aug 06 '15 08:08 colin-lamed

I think extending in something like this direction would be good... and yes as for your second point, would be good to be consistent with query-params syntax, fewer surprises.

rossm-memiah avatar Aug 06 '15 08:08 rossm-memiah

Looking at this more recently I'm of the opinion that if you just want the simplest possible random or indexed selection by default we have introduced a significant config overhead at the codex level. I'm inclined to suggest a strategy for body response selection should probably be defined outside of the codex.

rossajmcd avatar Sep 07 '16 22:09 rossajmcd