schemars icon indicating copy to clipboard operation
schemars copied to clipboard

support extension attribute

Open ahl opened this issue 5 years ago • 2 comments
trafficstars

It would be useful to be able to annotate a container with something like #[schemars(extension = "x-foo-bar")] and have those values appear in SchemaObject::extensions. I'd be happy to submit a PR if you'd be interested in it.

ahl avatar Aug 29 '20 07:08 ahl

This makes sense to me, except that extension values would need both a name and a value, so I imagine it would be more like #[schemars(extension("x-foo-bar" = "baz"))].

I'd just need to decide two main things:

  1. Whether the keys should be quoted, i.e. "x-foo-bar" = "baz" vs x-foo-bar = "baz". I think they would need to be quoted, mainly because parsing something like x-foo in syn would be quite difficult as it's not a valid rust identifier While x- prefixes aren't required in JSON schema extension attributes, they do seem to be fairly common in practice, so they'll need to be supported.
  2. What would a valid value be - a string literal, or any literal, or maybe any expression? Allowing an expression would be nice, so you could pass it a global variable or a function that returns anything implementing serde::Serialize - this would look like: #[schemars(extension("x-foo-bar" = something()))].

GREsau avatar Apr 11 '21 17:04 GREsau

  1. Agreed on quotes
  2. I like the idea of specifying an expression

I'd be happy to submit a PR if you'd be interested

ahl avatar Aug 19 '21 00:08 ahl