-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ToSchema Value
#86
Comments
I don't think that's a good idea for reasons similar to bytestrings. If your servant API accepts/responds with just |
I'd like to have
|
Fine then. Are you willing to make a PR? |
I just got bitten by lack of this instance. My usecase -- supporting Posgres' JSONB in my client-facing JSON API as well. |
@saurabhnanda is there a reason you can't use a E.g. we have something like this to be able collect some extra info we haven't added to the API yet: newtype Extra = Extra { getExtra :: Object }
deriving (Eq, Show, ToJSON, FromJSON)
instance ToSchema Extra where
declareNamedSchema _ = pure $ NamedSchema (Just "Extra") $ mempty
& type_ .~ SwaggerObject
& description ?~ "Arbitrary JSON object with extra information." So far the only reason for having this instance is |
I might be confused. What's the difference between Value and Object? What
would be used to represent a well-formed JSON?
…On 23 Dec 2016 7:16 pm, "Nickolay Kudasov" ***@***.***> wrote:
@saurabhnanda <https://github.com/saurabhnanda> is there a reason you
can't use a newtype wrapper around Value?
Does it have to be any Value or is it Object really?
E.g. we have something like this to be able collect some extra info we
haven't added to the API yet:
newtype Extra = Extra { getExtra :: Object }
deriving (Eq, Show, ToJSON, FromJSON)
instance ToSchema Extra where
declareNamedSchema _ = pure $ NamedSchema (Just "Extra") $ mempty
& type_ .~ SwaggerObject
& description ?~ "Arbitrary JSON object with extra information."
So far the only reason for having this instance is Value is buried deep
in the objects and you might not have the access to wrap it in a newtype.
I am okay with adding an instance for this use case, though, so PR's
welcome :)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#86 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AABu0amVmmTLUUb6-yn6Lu20_9suMaSDks5rK9DDgaJpZM4Kevvv>
.
|
Object has an object as the outer constructor, i.s. it's not an array or plain number or string. AFAIK, jsonb can be "just an int", so Value is correct one |
Yes, One reason is that it is not clear how to combine two Another is that if you use |
For my use-case it should be Object then, not Value. And do we already have
a ToSchema instance for Object?
…On 24 Dec 2016 3:39 am, "Nickolay Kudasov" ***@***.***> wrote:
Yes, Object can only be an object (i.e. a map) while Value can also be an
array, a string, a number, a boolean or null. In most cases you want
Object and not Value. Even if you're storing it in jsonb in PostgreSQL
database.
One reason is that it is not clear how to combine two Values (e.g. when
an update comes) while it is fairly straightforward to combine two Objects
(merge or deep merge).
Another is that if you use Value you're bound to check that it's not a
primitive or array before extracting useful information. This makes
analysis of this extra data a bit more complex than it should in my opinion.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#86 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AABu0R29AkmHR_a8iawA-26tnxy2_YYMks5rLEaTgaJpZM4Kevvv>
.
|
@saurabhnanda no, not yet. It's simple to define one locally for now: instance ToSchema Object where
declareNamedSchema _ = pure $ NamedSchema (Just "Object") $ mempty
& type_ .~ SwaggerObject
& description ?~ "Arbitrary JSON object." |
@fizruk this instance above doesn't seem to be valid for version 2.2 of the library - is there any workaround? |
@qrilka the instance is valid, but is overlapping with a more general one for instance {-# OVERLAPPING #-} ToSchema Object where
declareNamedSchema _ = pure $ NamedSchema (Just "Object") $ mempty
& type_ .~ SwaggerObject
& description ?~ "Arbitrary JSON object." |
using your instance above:
|
|
Yeah, I see what I did there with "not mentioned" fields.
Interesting, I didn't know |
As of now we've downgraded to version 2.1.6 but actually this new strictness in validation showed up minor inconsistency in one of our legacy endpoints which actually needs to be resolved. |
I think we can add an instance for |
AFAICS there isn't one. It's not that useful, but good to have for completeness
The text was updated successfully, but these errors were encountered: