>>> jsonref = object ["$ref" .= String "#/components/schemas/Foo"]
>>> fromJSON @Reference jsonref
Success (Reference {getReference = "#/components/schemas/Foo"})
>>> fromJSON @(Referenced Schema) jsonref
Success (Ref (Reference {getReference = "Foo"}))
This is not only confusing, but also makes it impossible to get the prefix back if I take the Reference out of a Referenced and try to render it back to JSON. Or dually, if I construct a Referenced with a Reference in it that has the prefix, it'll result in a malformed fragment specifier:
>>> jsonref = object ["$ref" .= String "#/components/schemas/Foo"]
>>> Success ref = fromJSON @Reference jsonref
>>> toJSON (Ref @Schema ref)
Object (fromList [("$ref",String "#/components/schemas/#/components/schemas/Foo")])
For the ToJSON instances, the special casing is implemented here:
|
referencedToJSON :: ToJSON a => Text -> Referenced a -> Value |
|
referencedToJSON prefix (Ref (Reference ref)) = object [ "$ref" .= (prefix <> ref) ] |
|
referencedToJSON _ (Inline x) = toJSON x |
|
|
|
instance ToJSON (Referenced Schema) where toJSON = referencedToJSON "#/components/schemas/" |
|
instance ToJSON (Referenced Param) where toJSON = referencedToJSON "#/components/parameters/" |
|
instance ToJSON (Referenced Response) where toJSON = referencedToJSON "#/components/responses/" |
|
instance ToJSON (Referenced RequestBody) where toJSON = referencedToJSON "#/components/requestBodies/" |
|
instance ToJSON (Referenced Example) where toJSON = referencedToJSON "#/components/examples/" |
|
instance ToJSON (Referenced Header) where toJSON = referencedToJSON "#/components/headers/" |
|
instance ToJSON (Referenced Link) where toJSON = referencedToJSON "#/components/links/" |
|
instance ToJSON (Referenced Callback) where toJSON = referencedToJSON "#/components/callbacks/" |
And there's an analogous section for FromJSON elsewhere in the same file. IMO these instances should just do the obvious thing and forward to the appropriate instances on Reference; things like prefix stripping can be done elsewhere.
This is not only confusing, but also makes it impossible to get the prefix back if I take the
Referenceout of aReferencedand try to render it back to JSON. Or dually, if I construct aReferencedwith aReferencein it that has the prefix, it'll result in a malformed fragment specifier:For the
ToJSONinstances, the special casing is implemented here:openapi3/src/Data/OpenApi/Internal.hs
Lines 1427 to 1438 in efb9a50
And there's an analogous section for
FromJSONelsewhere in the same file. IMO these instances should just do the obvious thing and forward to the appropriate instances onReference; things like prefix stripping can be done elsewhere.