Skip to content

Commit 23caa64

Browse files
committed
Add Callback and Link
1 parent c9830de commit 23caa64

File tree

4 files changed

+92
-7
lines changed

4 files changed

+92
-7
lines changed

src/Data/Swagger.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ module Data.Swagger (
8686
Responses(..),
8787
Response(..),
8888
HttpStatusCode,
89+
Link(..),
90+
Callback(..),
8991

9092
-- ** Security
9193
SecurityScheme(..),

src/Data/Swagger/Internal.hs

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ data Components = Components
196196
, _componentsRequestBodies :: Definitions RequestBody
197197
, _componentsHeader :: Definitions Header
198198
, _componentsSecuritySchemes :: Definitions SecurityScheme
199-
-- , _componentsLinks
200-
-- , _componentsCallbacks
199+
, _componentsLinks :: Definitions Link
200+
, _componentsCallbacks :: Definitions Callback
201201
} deriving (Eq, Show, Generic, Data, Typeable)
202202

203203
-- | Describes the operations available on a single path.
@@ -287,7 +287,11 @@ data Operation = Operation
287287
-- | The list of possible responses as they are returned from executing this operation.
288288
, _operationResponses :: Responses
289289

290-
-- TODO callbacks
290+
-- | A map of possible out-of band callbacks related to the parent operation.
291+
-- The key is a unique identifier for the 'Callback' Object.
292+
-- Each value in the map is a 'Callback' Object that describes a request
293+
-- that may be initiated by the API provider and the expected responses.
294+
, _operationCallbacks :: InsOrdHashMap Text (Referenced Callback)
291295

292296
-- | Declares this operation to be deprecated.
293297
-- Usage of the declared operation should be refrained.
@@ -519,6 +523,42 @@ data Example = Example
519523
, _exampleExternalValue :: Maybe URL
520524
} deriving (Eq, Show, Generic, Typeable, Data)
521525

526+
data ExpressionOrValue
527+
= Expression Text
528+
| Value Value
529+
deriving (Eq, Show, Generic, Typeable, Data)
530+
531+
-- | The Link object represents a possible design-time link for a response.
532+
-- The presence of a link does not guarantee the caller's ability to successfully invoke it,
533+
-- rather it provides a known relationship and traversal mechanism between responses and other operations.
534+
data Link = Link
535+
{ -- | A relative or absolute URI reference to an OAS operation.
536+
-- This field is mutually exclusive of the '_linkOperationId' field,
537+
-- and MUST point to an 'Operation' Object. Relative '_linkOperationRef'
538+
-- values MAY be used to locate an existing 'Operation' Object in the OpenAPI definition.
539+
_linkOperationRef :: Maybe Text
540+
541+
-- | The name of an /existing/, resolvable OAS operation, as defined with a unique
542+
-- '_operationOperationId'. This field is mutually exclusive of the '_linkOperationRef' field.
543+
, _linkOperationId :: Maybe Text
544+
545+
-- | A map representing parameters to pass to an operation as specified with '_linkOperationId'
546+
-- or identified via '_linkOperationRef'. The key is the parameter name to be used, whereas
547+
-- the value can be a constant or an expression to be evaluated and passed to the linked operation.
548+
-- The parameter name can be qualified using the parameter location @[{in}.]{name}@
549+
-- for operations that use the same parameter name in different locations (e.g. path.id).
550+
, _linkParameters :: InsOrdHashMap Text ExpressionOrValue
551+
552+
-- | A literal value or @{expression}@ to use as a request body when calling the target operation.
553+
, _linkRequestBody :: Maybe ExpressionOrValue
554+
555+
-- | A description of the link.
556+
, _linkDescription :: Maybe Text
557+
558+
-- | A server object to be used by the target operation.
559+
, _linkServer :: Maybe Server
560+
} deriving (Eq, Show, Generic, Typeable, Data)
561+
522562
-- | Items for @'SwaggerArray'@ schemas.
523563
--
524564
-- __Warning__: OpenAPI 3.0 does not support tuple arrays. However, OpenAPI 3.1 will, as
@@ -690,19 +730,29 @@ data Response = Response
690730
-- | Maps a header name to its definition.
691731
, _responseHeaders :: InsOrdHashMap HeaderName (Referenced Header)
692732

693-
-- TODO links
733+
-- | A map of operations links that can be followed from the response.
734+
-- The key of the map is a short name for the link, following the naming
735+
-- constraints of the names for 'Component' Objects.
736+
, _responseLinks :: InsOrdHashMap Text (Referenced Link)
694737
} deriving (Eq, Show, Generic, Data, Typeable)
695738

696739
instance IsString Response where
697-
fromString s = Response (fromString s) mempty mempty
740+
fromString s = Response (fromString s) mempty mempty mempty
741+
742+
-- | A map of possible out-of band callbacks related to the parent operation.
743+
-- Each value in the map is a 'PathItem' Object that describes a set of requests that
744+
-- may be initiated by the API provider and the expected responses.
745+
-- The key value used to identify the path item object is an expression, evaluated at runtime,
746+
-- that identifies a URL to use for the callback operation.
747+
newtype Callback = Callback (InsOrdHashMap Text PathItem)
748+
deriving (Eq, Show, Generic, Data, Typeable)
698749

699750
type HeaderName = Text
700751

701-
702752
-- TODO this is mostly a copy of 'Param'.
703753
data Header = Header
704754
{ -- | A short description of the header.
705-
_headerDescription :: Maybe Text
755+
_headerDescription :: Maybe HeaderName
706756

707757
, _headerSchema :: Maybe (Referenced Schema)
708758
} deriving (Eq, Show, Generic, Data, Typeable)
@@ -873,6 +923,7 @@ deriveGeneric ''ParamSchema
873923
deriveGeneric ''Swagger
874924
deriveGeneric ''Example
875925
deriveGeneric ''Encoding
926+
deriveGeneric ''Link
876927

877928
-- =======================================================================
878929
-- Monoid instances
@@ -1264,6 +1315,10 @@ instance ToJSON Encoding where
12641315
toJSON = sopSwaggerGenericToJSON
12651316
toEncoding = sopSwaggerGenericToEncoding
12661317

1318+
instance ToJSON Link where
1319+
toJSON = sopSwaggerGenericToJSON
1320+
toEncoding = sopSwaggerGenericToEncoding
1321+
12671322
instance ToJSON SecurityDefinitions where
12681323
toJSON (SecurityDefinitions sd) = toJSON sd
12691324

@@ -1280,6 +1335,8 @@ instance ToJSON (Referenced Response) where toJSON = referencedToJSON "#/compone
12801335
instance ToJSON (Referenced RequestBody) where toJSON = referencedToJSON "#/components/requestBodies/"
12811336
instance ToJSON (Referenced Example) where toJSON = referencedToJSON "#/components/examples/"
12821337
instance ToJSON (Referenced Header) where toJSON = referencedToJSON "#/components/headers/"
1338+
instance ToJSON (Referenced Link) where toJSON = referencedToJSON "#/components/links/"
1339+
instance ToJSON (Referenced Callback) where toJSON = referencedToJSON "#/components/callbacks/"
12831340

12841341
instance ToJSON ParamSchema where
12851342
-- TODO: this is a bit fishy, why we need sub object only in `ToJSON`?
@@ -1290,6 +1347,13 @@ instance ToJSON AdditionalProperties where
12901347
toJSON (AdditionalPropertiesAllowed b) = toJSON b
12911348
toJSON (AdditionalPropertiesSchema s) = toJSON s
12921349

1350+
instance ToJSON ExpressionOrValue where
1351+
toJSON (Expression expr) = toJSON expr
1352+
toJSON (Value val) = toJSON val
1353+
1354+
instance ToJSON Callback where
1355+
toJSON (Callback ps) = toJSON ps
1356+
12931357
-- =======================================================================
12941358
-- Manual FromJSON instances
12951359
-- =======================================================================
@@ -1385,6 +1449,9 @@ instance FromJSON MediaTypeObject where
13851449
instance FromJSON Encoding where
13861450
parseJSON = sopSwaggerGenericParseJSON
13871451

1452+
instance FromJSON Link where
1453+
parseJSON = sopSwaggerGenericParseJSON
1454+
13881455
instance FromJSON Reference where
13891456
parseJSON (Object o) = Reference <$> o .: "$ref"
13901457
parseJSON _ = empty
@@ -1408,6 +1475,8 @@ instance FromJSON (Referenced Response) where parseJSON = referencedParseJSON "#
14081475
instance FromJSON (Referenced RequestBody) where parseJSON = referencedParseJSON "#/components/requestBodies/"
14091476
instance FromJSON (Referenced Example) where parseJSON = referencedParseJSON "#/components/examples/"
14101477
instance FromJSON (Referenced Header) where parseJSON = referencedParseJSON "#/components/headers/"
1478+
instance FromJSON (Referenced Link) where parseJSON = referencedParseJSON "#/components/links/"
1479+
instance FromJSON (Referenced Callback) where parseJSON = referencedParseJSON "#/components/callbacks/"
14111480

14121481
instance FromJSON Xml where
14131482
parseJSON = genericParseJSON (jsonPrefix "xml")
@@ -1419,6 +1488,14 @@ instance FromJSON AdditionalProperties where
14191488
parseJSON (Bool b) = pure $ AdditionalPropertiesAllowed b
14201489
parseJSON js = AdditionalPropertiesSchema <$> parseJSON js
14211490

1491+
-- | All strings are parsed as expressions
1492+
instance FromJSON ExpressionOrValue where
1493+
parseJSON (String expr) = pure $ Expression expr
1494+
parseJSON v = pure $ Value v
1495+
1496+
instance FromJSON Callback where
1497+
parseJSON = fmap Callback . parseJSON
1498+
14221499
instance HasSwaggerAesonOptions Server where
14231500
swaggerAesonOptions _ = mkSwaggerAesonOptions "server"
14241501
instance HasSwaggerAesonOptions Components where
@@ -1454,6 +1531,9 @@ instance HasSwaggerAesonOptions Example where
14541531
instance HasSwaggerAesonOptions Encoding where
14551532
swaggerAesonOptions _ = mkSwaggerAesonOptions "encoding"
14561533

1534+
instance HasSwaggerAesonOptions Link where
1535+
swaggerAesonOptions _ = mkSwaggerAesonOptions "link"
1536+
14571537
instance HasSwaggerAesonOptions ParamSchema where
14581538
swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema"
14591539

@@ -1471,3 +1551,4 @@ instance AesonDefaultValue SwaggerType
14711551
instance AesonDefaultValue MimeList where defaultValue = Just mempty
14721552
instance AesonDefaultValue Info
14731553
instance AesonDefaultValue ParamLocation
1554+
instance AesonDefaultValue Link

src/Data/Swagger/Lens.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ makeFields ''ExternalDocs
5959
makeFields ''Encoding
6060
makeFields ''Example
6161
makeFields ''Discriminator
62+
makeFields ''Link
6263

6364
-- * Prisms
6465
-- ** 'SecuritySchemeType' prisms

src/Data/Swagger/Optics.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ makeFieldLabels ''ExternalDocs
9797
makeFieldLabels ''Encoding
9898
makeFieldLabels ''Example
9999
makeFieldLabels ''Discriminator
100+
makeFieldLabels ''Link
100101

101102
-- Prisms
102103

0 commit comments

Comments
 (0)