git.iko.soy/typeable/openapi3
on master help sign in
Merge ParamSchema into Schema
commit11f8f97ebb3ba7f8bda1a93af771ad9496e04115 authorMaxim Koltsov <kolmax94@gmail.com> dateMon 3 Aug 2020, 17:11 (6 years ago) parent4fd02b4
11 files changed+129−295
src/Data/Swagger.hs+2−3 src/Data/Swagger/Internal.hs+31−54 src/Data/Swagger/Internal/ParamSchema.hs+22−16 src/Data/Swagger/Internal/Schema.hs+7−26 src/Data/Swagger/Internal/Schema/Validation.hs+16−22 src/Data/Swagger/Lens.hs+29−39 src/Data/Swagger/Optics.hs+17−125 src/Data/Swagger/ParamSchema.hs+3−3 src/Data/Swagger/Schema.hs−5 src/Data/Swagger/Schema/Generator.hs+1−1 test/Data/Swagger/ParamSchemaSpec.hs+1−1
src/Data/Swagger.hs+2−3
@@ -73,7 +73,6 @@ module Data.Swagger (
7373 Encoding(..),
7474
7575 -- ** Schemas
76− ParamSchema(..),
7776 Schema(..),
7877 NamedSchema(..),
7978 SwaggerItems(..),
@@ -220,7 +219,7 @@ import Data.Swagger.Internal
220219-- & type_ ?~ SwaggerBoolean
221220-- & description ?~ "To be or not to be"
222221-- :}
223−-- {"description":"To be or not to be","type":"boolean"}
222+-- {"type":"boolean","description":"To be or not to be"}
224223--
225224-- Additionally, to simplify working with @'Response'@, both @'Operation'@ and @'Responses'@
226225-- have direct access to it via @'at' code@. Example:
@@ -279,7 +278,7 @@ import Data.Swagger.Internal
279278-- >>> BSL.putStrLn $ encode (Person "David" 28)
280279-- {"age":28,"name":"David"}
281280-- >>> BSL.putStrLn $ encode $ toSchema (Proxy :: Proxy Person)
282−-- {"required":["name","age"],"properties":{"name":{"type":"string"},"age":{"type":"integer"}},"type":"object"}
281+-- {"required":["name","age"],"type":"object","properties":{"age":{"type":"integer"},"name":{"type":"string"}}}
283282--
284283-- This package implements OpenAPI 3.0 spec, which supports @oneOf@ in schemas, allowing any sum types
285284-- to be faithfully represented. All sum encodings supported by @aeson@ are supported here as well, with
src/Data/Swagger/Internal.hs+31−54
@@ -630,9 +630,33 @@ data Schema = Schema
630630 , _schemaMaxProperties :: Maybe Integer
631631 , _schemaMinProperties :: Maybe Integer
632632
633− , _schemaParamSchema :: ParamSchema
633+ , -- | Declares the value of the parameter that the server will use if none is provided,
634+ -- for example a @"count"@ to control the number of results per page might default to @100@
635+ -- if not supplied by the client in the request.
636+ -- (Note: "default" has no meaning for required parameters.)
637+ -- Unlike JSON Schema this value MUST conform to the defined type for this parameter.
638+ _schemaDefault :: Maybe Value
639+
640+ , _schemaType :: Maybe SwaggerType
641+ , _schemaFormat :: Maybe Format
642+ , _schemaItems :: Maybe SwaggerItems
643+ , _schemaMaximum :: Maybe Scientific
644+ , _schemaExclusiveMaximum :: Maybe Bool
645+ , _schemaMinimum :: Maybe Scientific
646+ , _schemaExclusiveMinimum :: Maybe Bool
647+ , _schemaMaxLength :: Maybe Integer
648+ , _schemaMinLength :: Maybe Integer
649+ , _schemaPattern :: Maybe Pattern
650+ , _schemaMaxItems :: Maybe Integer
651+ , _schemaMinItems :: Maybe Integer
652+ , _schemaUniqueItems :: Maybe Bool
653+ , _schemaEnum :: Maybe [Value]
654+ , _schemaMultipleOf :: Maybe Scientific
634655 } deriving (Eq, Show, Generic, Data, Typeable)
635656
657+-- | Regex pattern for @string@ type.
658+type Pattern = Text
659+
636660data Discriminator = Discriminator
637661 { -- | The name of the property in the payload that will hold the discriminator value.
638662 _discriminatorPropertyName :: Text
@@ -648,34 +672,6 @@ data NamedSchema = NamedSchema
648672 , _namedSchemaSchema :: Schema
649673 } deriving (Eq, Show, Generic, Data, Typeable)
650674
651−-- | Regex pattern for @string@ type.
652−type Pattern = Text
653−
654−data ParamSchema = ParamSchema
655− { -- | Declares the value of the parameter that the server will use if none is provided,
656− -- for example a @"count"@ to control the number of results per page might default to @100@
657− -- if not supplied by the client in the request.
658− -- (Note: "default" has no meaning for required parameters.)
659− -- Unlike JSON Schema this value MUST conform to the defined type for this parameter.
660− _paramSchemaDefault :: Maybe Value
661−
662− , _paramSchemaType :: Maybe SwaggerType
663− , _paramSchemaFormat :: Maybe Format
664− , _paramSchemaItems :: Maybe SwaggerItems
665− , _paramSchemaMaximum :: Maybe Scientific
666− , _paramSchemaExclusiveMaximum :: Maybe Bool
667− , _paramSchemaMinimum :: Maybe Scientific
668− , _paramSchemaExclusiveMinimum :: Maybe Bool
669− , _paramSchemaMaxLength :: Maybe Integer
670− , _paramSchemaMinLength :: Maybe Integer
671− , _paramSchemaPattern :: Maybe Pattern
672− , _paramSchemaMaxItems :: Maybe Integer
673− , _paramSchemaMinItems :: Maybe Integer
674− , _paramSchemaUniqueItems :: Maybe Bool
675− , _paramSchemaEnum :: Maybe [Value]
676− , _paramSchemaMultipleOf :: Maybe Scientific
677− } deriving (Eq, Show, Generic, Typeable, Data)
678−
679675data Xml = Xml
680676 { -- | Replaces the name of the element/attribute used for the described schema property.
681677 -- When defined within the @'SwaggerItems'@ (items), it will affect the name of the individual XML elements within the list.
@@ -934,7 +930,6 @@ deriveGeneric ''MediaTypeObject
934930deriveGeneric ''Responses
935931deriveGeneric ''SecurityScheme
936932deriveGeneric ''Schema
937−deriveGeneric ''ParamSchema
938933deriveGeneric ''Swagger
939934deriveGeneric ''Example
940935deriveGeneric ''Encoding
@@ -980,12 +975,6 @@ instance Monoid Schema where
980975 mempty = genericMempty
981976 mappend = (<>)
982977
983−instance Semigroup ParamSchema where
984− (<>) = genericMappend
985−instance Monoid ParamSchema where
986− mempty = genericMempty
987− mappend = (<>)
988−
989978instance Semigroup Param where
990979 (<>) = genericMappend
991980instance Monoid Param where
@@ -1080,7 +1069,6 @@ instance SwaggerMonoid Info
10801069instance SwaggerMonoid Components
10811070instance SwaggerMonoid PathItem
10821071instance SwaggerMonoid Schema
1083−instance SwaggerMonoid ParamSchema
10841072instance SwaggerMonoid Param
10851073instance SwaggerMonoid Responses
10861074instance SwaggerMonoid Response
@@ -1265,8 +1253,8 @@ instance ToJSON SecurityScheme where
12651253 toEncoding = sopSwaggerGenericToEncoding
12661254
12671255instance ToJSON Schema where
1268− toJSON = sopSwaggerGenericToJSON
1269− toEncoding = sopSwaggerGenericToEncoding
1256+ toJSON = sopSwaggerGenericToJSONWithOpts $
1257+ mkSwaggerAesonOptions "schema" & saoSubObject ?~ "items"
12701258
12711259instance ToJSON Header where
12721260 toJSON = sopSwaggerGenericToJSON
@@ -1353,11 +1341,6 @@ instance ToJSON (Referenced Header) where toJSON = referencedToJSON "#/compone
13531341instance ToJSON (Referenced Link) where toJSON = referencedToJSON "#/components/links/"
13541342instance ToJSON (Referenced Callback) where toJSON = referencedToJSON "#/components/callbacks/"
13551343
1356−instance ToJSON ParamSchema where
1357− -- TODO: this is a bit fishy, why we need sub object only in `ToJSON`?
1358− toJSON = sopSwaggerGenericToJSONWithOpts $
1359− mkSwaggerAesonOptions "paramSchema" & saoSubObject ?~ "items"
1360−
13611344instance ToJSON AdditionalProperties where
13621345 toJSON (AdditionalPropertiesAllowed b) = toJSON b
13631346 toJSON (AdditionalPropertiesSchema s) = toJSON s
@@ -1409,10 +1392,11 @@ instance FromJSON SecurityScheme where
14091392instance FromJSON Schema where
14101393 parseJSON = fmap nullaryCleanup . sopSwaggerGenericParseJSON
14111394 where nullaryCleanup :: Schema -> Schema
1412− nullaryCleanup s@Schema{_schemaParamSchema=ps} =
1413− if _paramSchemaItems ps == Just (SwaggerItemsArray [])
1395+ nullaryCleanup s =
1396+ if _schemaItems s == Just (SwaggerItemsArray [])
14141397 then s { _schemaExample = Nothing
1415− , _schemaParamSchema = ps { _paramSchemaMaxItems = Nothing } }
1398+ , _schemaMaxItems = Nothing
1399+ }
14161400 else s
14171401
14181402instance FromJSON Header where
@@ -1496,9 +1480,6 @@ instance FromJSON (Referenced Callback) where parseJSON = referencedParseJSON "#
14961480instance FromJSON Xml where
14971481 parseJSON = genericParseJSON (jsonPrefix "xml")
14981482
1499−instance FromJSON ParamSchema where
1500− parseJSON = sopSwaggerGenericParseJSON
1501−
15021483instance FromJSON AdditionalProperties where
15031484 parseJSON (Bool b) = pure $ AdditionalPropertiesAllowed b
15041485 parseJSON js = AdditionalPropertiesSchema <$> parseJSON js
@@ -1549,12 +1530,8 @@ instance HasSwaggerAesonOptions Encoding where
15491530instance HasSwaggerAesonOptions Link where
15501531 swaggerAesonOptions _ = mkSwaggerAesonOptions "link"
15511532
1552−instance HasSwaggerAesonOptions ParamSchema where
1553− swaggerAesonOptions _ = mkSwaggerAesonOptions "paramSchema"
1554−
15551533instance AesonDefaultValue Server
15561534instance AesonDefaultValue Components
1557−instance AesonDefaultValue ParamSchema
15581535instance AesonDefaultValue OAuth2ImplicitFlow
15591536instance AesonDefaultValue OAuth2PasswordFlow
15601537instance AesonDefaultValue OAuth2ClientCredentialsFlow
src/Data/Swagger/Internal/ParamSchema.hs+22−16
@@ -50,25 +50,31 @@ import qualified Data.ByteString.Lazy as BSL
5050import GHC.TypeLits (TypeError, ErrorMessage(..))
5151
5252-- | Default schema for binary data (any sequence of octets).
53−binaryParamSchema :: ParamSchema
54−binaryParamSchema = mempty
53+binarySchema :: Schema
54+binarySchema = mempty
5555 & type_ ?~ SwaggerString
5656 & format ?~ "binary"
5757
5858-- | Default schema for binary data (base64 encoded).
59−byteParamSchema :: ParamSchema
60−byteParamSchema = mempty
59+byteSchema :: Schema
60+byteSchema = mempty
6161 & type_ ?~ SwaggerString
6262 & format ?~ "byte"
6363
6464-- | Default schema for password string.
6565-- @"password"@ format is used to hint UIs the input needs to be obscured.
66−passwordParamSchema :: ParamSchema
67−passwordParamSchema = mempty
66+passwordSchema :: Schema
67+passwordSchema = mempty
6868 & type_ ?~ SwaggerString
6969 & format ?~ "password"
7070
71−-- | Convert a type into a plain @'ParamSchema'@.
71+-- | Convert a type into a plain @'Schema'@.
72+--
73+-- In previous versions of the package there was a separate type called @ParamSchema@, which was
74+-- included in a greater 'Schema'. Now this is a single class, but distinction for schema generators
75+-- for "simple" types is preserved.
76+--
77+-- 'ToParamSchema' is suited only for primitive-like types without nested fields and such.
7278--
7379-- An example type and instance:
7480--
@@ -108,8 +114,8 @@ class ToParamSchema a where
108114 --
109115 -- >>> encode $ toParamSchema (Proxy :: Proxy Integer)
110116 -- "{\"type\":\"integer\"}"
111− toParamSchema :: Proxy a -> ParamSchema
112− default toParamSchema :: (Generic a, GToParamSchema (Rep a)) => Proxy a -> ParamSchema
117+ toParamSchema :: Proxy a -> Schema
118+ default toParamSchema :: (Generic a, GToParamSchema (Rep a)) => Proxy a -> Schema
113119 toParamSchema = genericToParamSchema defaultSchemaOptions
114120
115121instance {-# OVERLAPPING #-} ToParamSchema String where
@@ -151,7 +157,7 @@ instance ToParamSchema Word64 where
151157--
152158-- >>> encode $ toParamSchemaBoundedIntegral (Proxy :: Proxy Int8)
153159-- "{\"maximum\":127,\"minimum\":-128,\"type\":\"integer\"}"
154−toParamSchemaBoundedIntegral :: forall a t. (Bounded a, Integral a) => Proxy a -> ParamSchema
160+toParamSchemaBoundedIntegral :: forall a t. (Bounded a, Integral a) => Proxy a -> Schema
155161toParamSchemaBoundedIntegral _ = mempty
156162 & type_ ?~ SwaggerInteger
157163 & minimum_ ?~ fromInteger (toInteger (minBound :: a))
@@ -181,7 +187,7 @@ instance ToParamSchema Float where
181187 & type_ ?~ SwaggerNumber
182188 & format ?~ "float"
183189
184−timeParamSchema :: String -> ParamSchema
190+timeParamSchema :: String -> Schema
185191timeParamSchema fmt = mempty
186192 & type_ ?~ SwaggerString
187193 & format ?~ T.pack fmt
@@ -254,7 +260,7 @@ instance ToParamSchema a => ToParamSchema (Identity a) where toParamSchema _ = t
254260instance ToParamSchema a => ToParamSchema [a] where
255261 toParamSchema _ = mempty
256262 & type_ ?~ SwaggerArray
257− & items ?~ SwaggerItemsObject (Inline $ mempty & paramSchema .~ toParamSchema (Proxy :: Proxy a))
263+ & items ?~ SwaggerItemsObject (Inline $ toParamSchema (Proxy :: Proxy a))
258264
259265instance ToParamSchema a => ToParamSchema (V.Vector a) where toParamSchema _ = toParamSchema (Proxy :: Proxy [a])
260266instance ToParamSchema a => ToParamSchema (VP.Vector a) where toParamSchema _ = toParamSchema (Proxy :: Proxy [a])
@@ -281,17 +287,17 @@ instance ToParamSchema UUID where
281287 & type_ ?~ SwaggerString
282288 & format ?~ "uuid"
283289
284−-- | A configurable generic @'ParamSchema'@ creator.
290+-- | A configurable generic @'Schema'@ creator.
285291--
286292-- >>> :set -XDeriveGeneric
287293-- >>> data Color = Red | Blue deriving Generic
288294-- >>> encode $ genericToParamSchema defaultSchemaOptions (Proxy :: Proxy Color)
289295-- "{\"type\":\"string\",\"enum\":[\"Red\",\"Blue\"]}"
290−genericToParamSchema :: forall a t. (Generic a, GToParamSchema (Rep a)) => SchemaOptions -> Proxy a -> ParamSchema
296+genericToParamSchema :: forall a t. (Generic a, GToParamSchema (Rep a)) => SchemaOptions -> Proxy a -> Schema
291297genericToParamSchema opts _ = gtoParamSchema opts (Proxy :: Proxy (Rep a)) mempty
292298
293299class GToParamSchema (f :: * -> *) where
294− gtoParamSchema :: SchemaOptions -> Proxy f -> ParamSchema -> ParamSchema
300+ gtoParamSchema :: SchemaOptions -> Proxy f -> Schema -> Schema
295301
296302instance GToParamSchema f => GToParamSchema (D1 d f) where
297303 gtoParamSchema opts _ = gtoParamSchema opts (Proxy :: Proxy f)
@@ -309,7 +315,7 @@ instance (GEnumParamSchema f, GEnumParamSchema g) => GToParamSchema (f :+: g) wh
309315 gtoParamSchema opts _ = genumParamSchema opts (Proxy :: Proxy (f :+: g))
310316
311317class GEnumParamSchema (f :: * -> *) where
312− genumParamSchema :: SchemaOptions -> Proxy f -> ParamSchema -> ParamSchema
318+ genumParamSchema :: SchemaOptions -> Proxy f -> Schema -> Schema
313319
314320instance (GEnumParamSchema f, GEnumParamSchema g) => GEnumParamSchema (f :+: g) where
315321 genumParamSchema opts _ = genumParamSchema opts (Proxy :: Proxy f) . genumParamSchema opts (Proxy :: Proxy g)
src/Data/Swagger/Internal/Schema.hs+7−26
@@ -289,25 +289,6 @@ inlineNonRecursiveSchemas defs = inlineSchemasWhen nonRecursive defs
289289 traverse_ usedNames (InsOrdHashMap.lookup name defs)
290290 Inline subschema -> usedNames subschema
291291
292−-- | Default schema for binary data (any sequence of octets).
293−binarySchema :: Schema
294−binarySchema = mempty
295− & type_ ?~ SwaggerString
296− & format ?~ "binary"
297−
298−-- | Default schema for binary data (base64 encoded).
299−byteSchema :: Schema
300−byteSchema = mempty
301− & type_ ?~ SwaggerString
302− & format ?~ "byte"
303−
304−-- | Default schema for password string.
305−-- @"password"@ format is used to hint UIs the input needs to be obscured.
306−passwordSchema :: Schema
307−passwordSchema = mempty
308− & type_ ?~ SwaggerString
309− & format ?~ "password"
310−
311292-- | Make an unrestrictive sketch of a @'Schema'@ based on a @'ToJSON'@ instance.
312293-- Produced schema can be used for further refinement.
313294--
@@ -323,7 +304,7 @@ passwordSchema = mempty
323304-- >>> data Person = Person { name :: String, age :: Int } deriving (Generic)
324305-- >>> instance ToJSON Person
325306-- >>> BSL.putStrLn $ encode $ sketchSchema (Person "Jack" 25)
326−-- {"required":["age","name"],"properties":{"age":{"type":"number"},"name":{"type":"string"}},"example":{"age":25,"name":"Jack"},"type":"object"}
307+-- {"example":{"age":25,"name":"Jack"},"required":["age","name"],"type":"object","properties":{"age":{"type":"number"},"name":{"type":"string"}}}
327308sketchSchema :: ToJSON a => a -> Schema
328309sketchSchema = sketch . toJSON
329310 where
@@ -367,7 +348,7 @@ sketchSchema = sketch . toJSON
367348-- >>> data Person = Person { name :: String, age :: Int } deriving (Generic)
368349-- >>> instance ToJSON Person
369350-- >>> BSL.putStrLn $ encode $ sketchStrictSchema (Person "Jack" 25)
370−-- {"required":["age","name"],"properties":{"age":{"maximum":25,"minimum":25,"multipleOf":25,"type":"number","enum":[25]},"name":{"maxLength":4,"pattern":"Jack","minLength":4,"type":"string","enum":["Jack"]}},"maxProperties":2,"minProperties":2,"type":"object","enum":[{"age":25,"name":"Jack"}]}
351+-- {"minProperties":2,"required":["age","name"],"maxProperties":2,"type":"object","enum":[{"age":25,"name":"Jack"}],"properties":{"age":{"maximum":25,"minimum":25,"multipleOf":25,"type":"number","enum":[25]},"name":{"maxLength":4,"pattern":"Jack","minLength":4,"type":"string","enum":["Jack"]}}}
371352sketchStrictSchema :: ToJSON a => a -> Schema
372353sketchStrictSchema = go . toJSON
373354 where
@@ -603,7 +584,7 @@ genericDeclareNamedSchemaNewtype opts f proxy = genericNameSchema opts proxy <$>
603584-- >>> instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (T.pack . show)
604585-- >>> type ImageUrl = T.Text
605586-- >>> BSL.putStrLn $ encode $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
606−-- {"properties":{"Neutral":{"type":"string"},"Focus":{"type":"string"},"Active":{"type":"string"},"Hover":{"type":"string"},"Disabled":{"type":"string"}},"type":"object"}
587+-- {"type":"object","properties":{"Focus":{"type":"string"},"Disabled":{"type":"string"},"Active":{"type":"string"},"Neutral":{"type":"string"},"Hover":{"type":"string"}}}
607588--
608589-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
609590-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
@@ -631,7 +612,7 @@ declareSchemaBoundedEnumKeyMapping _ = case toJSONKey :: ToJSONKeyFunction key o
631612-- >>> instance ToJSONKey ButtonState where toJSONKey = toJSONKeyText (T.pack . show)
632613-- >>> type ImageUrl = T.Text
633614-- >>> BSL.putStrLn $ encode $ toSchemaBoundedEnumKeyMapping (Proxy :: Proxy (Map ButtonState ImageUrl))
634−-- {"properties":{"Neutral":{"type":"string"},"Focus":{"type":"string"},"Active":{"type":"string"},"Hover":{"type":"string"},"Disabled":{"type":"string"}},"type":"object"}
615+-- {"type":"object","properties":{"Focus":{"type":"string"},"Disabled":{"type":"string"},"Active":{"type":"string"},"Neutral":{"type":"string"},"Hover":{"type":"string"}}}
635616--
636617-- Note: this is only useful when @key@ is encoded with 'ToJSONKeyText'.
637618-- If it is encoded with 'ToJSONKeyValue' then a regular schema for @[(key, value)]@ is used.
@@ -667,14 +648,14 @@ gdatatypeSchemaName opts _ = case orig of
667648 orig = datatypeName (Proxy3 :: Proxy3 d f a)
668649 name = datatypeNameModifier opts orig
669650
670−-- | Lift a plain @'ParamSchema'@ into a model @'NamedSchema'@.
651+-- | Construct 'NamedSchema' usinng 'ToParamSchema'.
671652paramSchemaToNamedSchema :: (ToParamSchema a, Generic a, Rep a ~ D1 d f, Datatype d) =>
672653 SchemaOptions -> Proxy a -> NamedSchema
673654paramSchemaToNamedSchema opts proxy = genericNameSchema opts proxy (paramSchemaToSchema proxy)
674655
675−-- | Lift a plain @'ParamSchema'@ into a model @'Schema'@.
656+-- | Construct 'Schema' usinng 'ToParamSchema'.
676657paramSchemaToSchema :: ToParamSchema a => Proxy a -> Schema
677−paramSchemaToSchema proxy = mempty & paramSchema .~ toParamSchema proxy
658+paramSchemaToSchema = toParamSchema
678659
679660nullarySchema :: Schema
680661nullarySchema = mempty
src/Data/Swagger/Internal/Schema/Validation.hs+16−22
@@ -205,7 +205,7 @@ instance Monad Result where
205205
206206-- | Validation configuration.
207207data Config = Config
208− { -- | Pattern checker for @'_paramSchemaPattern'@ validation.
208+ { -- | Pattern checker for @'_schemaPattern'@ validation.
209209 configPatternChecker :: Pattern -> Text -> Bool
210210 -- | Schema definitions in scope to resolve references.
211211 , configDefinitions :: Definitions Schema
@@ -300,21 +300,15 @@ validateWithSchemaRef (Inline s) js = sub s (validateWithSchema js)
300300validateWithSchema :: Value -> Validation Schema ()
301301validateWithSchema val = do
302302 validateSchemaType val
303− sub_ paramSchema $ validateEnum val
304−
305−-- | Validate JSON @'Value'@ with Swagger @'ParamSchema'@.
306−validateWithParamSchema :: Value -> Validation ParamSchema ()
307−validateWithParamSchema val = do
308− validateParamSchemaType val
309303 validateEnum val
310304
311−validateInteger :: Scientific -> Validation ParamSchema ()
305+validateInteger :: Scientific -> Validation Schema ()
312306validateInteger n = do
313307 when (not (isInteger n)) $
314308 invalid ("not an integer")
315309 validateNumber n
316310
317−validateNumber :: Scientific -> Validation ParamSchema ()
311+validateNumber :: Scientific -> Validation Schema ()
318312validateNumber n = withConfig $ \_cfg -> withSchema $ \sch -> do
319313 let exMax = Just True == sch ^. exclusiveMaximum
320314 exMin = Just True == sch ^. exclusiveMinimum
@@ -331,7 +325,7 @@ validateNumber n = withConfig $ \_cfg -> withSchema $ \sch -> do
331325 when (not (isInteger (n / k))) $
332326 invalid ("expected a multiple of " ++ show k ++ " but got " ++ show n)
333327
334−validateString :: Text -> Validation ParamSchema ()
328+validateString :: Text -> Validation Schema ()
335329validateString s = do
336330 check maxLength $ \n ->
337331 when (len > fromInteger n) $
@@ -348,7 +342,7 @@ validateString s = do
348342 where
349343 len = Text.length s
350344
351−validateArray :: Vector Value -> Validation ParamSchema ()
345+validateArray :: Vector Value -> Validation Schema ()
352346validateArray xs = do
353347 check maxItems $ \n ->
354348 when (len > fromInteger n) $
@@ -418,7 +412,7 @@ validateObject o = withSchema $ \sch ->
418412 unknownProperty pname = invalid $
419413 "property " <> show pname <> " is found in JSON value, but it is not mentioned in Swagger schema"
420414
421−validateEnum :: Value -> Validation ParamSchema ()
415+validateEnum :: Value -> Validation Schema ()
422416validateEnum val = do
423417 check enum_ $ \xs ->
424418 when (val `notElem` xs) $
@@ -431,7 +425,7 @@ validateEnum val = do
431425-- >>> inferSchemaTypes <$> decode "{\"minProperties\": 1}"
432426-- Just [SwaggerObject]
433427inferSchemaTypes :: Schema -> [SwaggerType]
434−inferSchemaTypes sch = inferParamSchemaTypes (sch ^. paramSchema) ++
428+inferSchemaTypes sch = inferParamSchemaTypes sch ++
435429 [ SwaggerObject | any ($ sch)
436430 [ has (additionalProperties._Just)
437431 , has (maxProperties._Just)
@@ -452,7 +446,7 @@ inferSchemaTypes sch = inferParamSchemaTypes (sch ^. paramSchema) ++
452446--
453447-- >>> inferSchemaTypes <$> decode "{\"minimum\": 1}"
454448-- Just [SwaggerInteger]
455−inferParamSchemaTypes :: ParamSchema -> [SwaggerType]
449+inferParamSchemaTypes :: Schema -> [SwaggerType]
456450inferParamSchemaTypes sch = concat
457451 [ [ SwaggerArray | any ($ sch)
458452 [ has (items._Just)
@@ -491,21 +485,21 @@ validateSchemaType val = withSchema $ \sch ->
491485 case (sch ^. type_, val) of
492486 (Just SwaggerNull, Null) -> valid
493487 (Just SwaggerBoolean, Bool _) -> valid
494− (Just SwaggerInteger, Number n) -> sub_ paramSchema (validateInteger n)
495− (Just SwaggerNumber, Number n) -> sub_ paramSchema (validateNumber n)
496− (Just SwaggerString, String s) -> sub_ paramSchema (validateString s)
497− (Just SwaggerArray, Array xs) -> sub_ paramSchema (validateArray xs)
488+ (Just SwaggerInteger, Number n) -> validateInteger n
489+ (Just SwaggerNumber, Number n) -> validateNumber n
490+ (Just SwaggerString, String s) -> validateString s
491+ (Just SwaggerArray, Array xs) -> validateArray xs
498492 (Just SwaggerObject, Object o) -> validateObject o
499493 (Nothing, Null) -> valid
500494 (Nothing, Bool _) -> valid
501495 -- Number by default
502− (Nothing, Number n) -> sub_ paramSchema (validateNumber n)
503− (Nothing, String s) -> sub_ paramSchema (validateString s)
504− (Nothing, Array xs) -> sub_ paramSchema (validateArray xs)
496+ (Nothing, Number n) -> validateNumber n
497+ (Nothing, String s) -> validateString s
498+ (Nothing, Array xs) -> validateArray xs
505499 (Nothing, Object o) -> validateObject o
506500 bad -> invalid $ "expected JSON value of type " ++ showType bad
507501
508−validateParamSchemaType :: Value -> Validation ParamSchema ()
502+validateParamSchemaType :: Value -> Validation Schema ()
509503validateParamSchemaType val = withSchema $ \sch ->
510504 case (sch ^. type_, val) of
511505 (Just SwaggerBoolean, Bool _) -> valid
src/Data/Swagger/Lens.hs+29−39
@@ -29,8 +29,6 @@ import Data.Text (Text)
2929makeFields ''Swagger
3030makeFields ''Components
3131makeFields ''Server
32−-- conflict with enum of ParamSchema
33−--makeLensesWith swaggerFieldRules ''ServerVariable
3432makeFields ''RequestBody
3533makeFields ''MediaTypeObject
3634makeFields ''Info
@@ -43,7 +41,6 @@ makeLensesWith swaggerFieldRules ''Param
4341makeFields ''Header
4442makeLensesWith swaggerFieldRules ''Schema
4543makeFields ''NamedSchema
46−makeLensesWith swaggerFieldRules ''ParamSchema
4744makeFields ''Xml
4845makeLensesWith swaggerFieldRules ''Responses
4946makeFields ''Response
@@ -102,75 +99,68 @@ instance At Responses where at n = responses . at n
10299instance Ixed Operation where ix n = responses . ix n
103100instance At Operation where at n = responses . at n
104101
105−instance HasParamSchema NamedSchema ParamSchema where paramSchema = schema.paramSchema
106−
107−-- HasType instances
108−instance HasType Schema (Maybe SwaggerType) where type_ = paramSchema.type_
109−instance HasType NamedSchema (Maybe SwaggerType) where type_ = paramSchema.type_
110−
111−-- HasDefault instances
112−instance HasDefault Schema (Maybe Value) where default_ = paramSchema.default_
102+instance HasType NamedSchema (Maybe SwaggerType) where type_ = schema.type_
113103
114104-- OVERLAPPABLE instances
115105
116106instance
117107 {-# OVERLAPPABLE #-}
118− HasParamSchema s ParamSchema
108+ HasSchema s Schema
119109 => HasFormat s (Maybe Format) where
120− format = paramSchema.format
110+ format = schema.format
121111
122112instance
123113 {-# OVERLAPPABLE #-}
124− HasParamSchema s ParamSchema
114+ HasSchema s Schema
125115 => HasItems s (Maybe SwaggerItems) where
126− items = paramSchema.items
116+ items = schema.items
127117
128118instance
129119 {-# OVERLAPPABLE #-}
130− HasParamSchema s ParamSchema
120+ HasSchema s Schema
131121 => HasMaximum s (Maybe Scientific) where
132− maximum_ = paramSchema.maximum_
122+ maximum_ = schema.maximum_
133123
134−instance {-# OVERLAPPABLE #-} HasParamSchema s ParamSchema
124+instance {-# OVERLAPPABLE #-} HasSchema s Schema
135125 => HasExclusiveMaximum s (Maybe Bool) where
136− exclusiveMaximum = paramSchema.exclusiveMaximum
126+ exclusiveMaximum = schema.exclusiveMaximum
137127
138−instance {-# OVERLAPPABLE #-} HasParamSchema s ParamSchema
128+instance {-# OVERLAPPABLE #-} HasSchema s Schema
139129 => HasMinimum s (Maybe Scientific) where
140− minimum_ = paramSchema.minimum_
130+ minimum_ = schema.minimum_
141131
142−instance {-# OVERLAPPABLE #-} HasParamSchema s ParamSchema
132+instance {-# OVERLAPPABLE #-} HasSchema s Schema
143133 => HasExclusiveMinimum s (Maybe Bool) where
144− exclusiveMinimum = paramSchema.exclusiveMinimum
134+ exclusiveMinimum = schema.exclusiveMinimum
145135
146−instance {-# OVERLAPPABLE #-} HasParamSchema s ParamSchema
136+instance {-# OVERLAPPABLE #-} HasSchema s Schema
147137 => HasMaxLength s (Maybe Integer) where
148− maxLength = paramSchema.maxLength
138+ maxLength = schema.maxLength
149139
150−instance {-# OVERLAPPABLE #-} HasParamSchema s ParamSchema
140+instance {-# OVERLAPPABLE #-} HasSchema s Schema
151141 => HasMinLength s (Maybe Integer) where
152− minLength = paramSchema.minLength
142+ minLength = schema.minLength
153143
154−instance {-# OVERLAPPABLE #-} HasParamSchema s ParamSchema
144+instance {-# OVERLAPPABLE #-} HasSchema s Schema
155145 => HasPattern s (Maybe Text) where
156− pattern = paramSchema.pattern
146+ pattern = schema.pattern
157147
158−instance {-# OVERLAPPABLE #-} HasParamSchema s ParamSchema
148+instance {-# OVERLAPPABLE #-} HasSchema s Schema
159149 => HasMaxItems s (Maybe Integer) where
160− maxItems = paramSchema.maxItems
150+ maxItems = schema.maxItems
161151
162−instance {-# OVERLAPPABLE #-} HasParamSchema s ParamSchema
152+instance {-# OVERLAPPABLE #-} HasSchema s Schema
163153 => HasMinItems s (Maybe Integer) where
164− minItems = paramSchema.minItems
154+ minItems = schema.minItems
165155
166−instance {-# OVERLAPPABLE #-} HasParamSchema s ParamSchema
156+instance {-# OVERLAPPABLE #-} HasSchema s Schema
167157 => HasUniqueItems s (Maybe Bool) where
168− uniqueItems = paramSchema.uniqueItems
158+ uniqueItems = schema.uniqueItems
169159
170−instance {-# OVERLAPPABLE #-} HasParamSchema s ParamSchema
160+instance {-# OVERLAPPABLE #-} HasSchema s Schema
171161 => HasEnum s (Maybe [Value]) where
172− enum_ = paramSchema.enum_
162+ enum_ = schema.enum_
173163
174−instance {-# OVERLAPPABLE #-} HasParamSchema s ParamSchema
164+instance {-# OVERLAPPABLE #-} HasSchema s Schema
175165 => HasMultipleOf s (Maybe Scientific) where
176− multipleOf = paramSchema.multipleOf
166+ multipleOf = schema.multipleOf
src/Data/Swagger/Optics.hs+17−125
@@ -43,7 +43,7 @@
4343-- & #type ?~ SwaggerBoolean
4444-- & #description ?~ "To be or not to be"
4545-- :}
46−-- {"description":"To be or not to be","type":"boolean"}
46+-- {"type":"boolean","description":"To be or not to be"}
4747--
4848-- Additionally, to simplify working with @'Response'@, both @'Operation'@ and
4949-- @'Responses'@ have direct access to it via @'Optics.Core.At.at'@. Example:
@@ -81,7 +81,6 @@ makeFieldLabels ''Param
8181makeFieldLabels ''Header
8282makeFieldLabels ''Schema
8383makeFieldLabels ''NamedSchema
84−makeFieldLabels ''ParamSchema
8584makeFieldLabels ''Xml
8685makeFieldLabels ''Responses
8786makeFieldLabels ''Response
@@ -153,239 +152,132 @@ instance At Operation where
153152 at n = #responses % at n
154153 {-# INLINE at #-}
155154
156−-- #paramSchema
157−
158−instance
159− ( a ~ ParamSchema
160− , b ~ ParamSchema
161− ) => LabelOptic "paramSchema" A_Lens NamedSchema NamedSchema a b where
162− labelOptic = #schema % #paramSchema
163− {-# INLINE labelOptic #-}
164−
165155-- #type
166156
167−instance
168− ( a ~ Maybe SwaggerType
169− , b ~ Maybe SwaggerType
170− ) => LabelOptic "type" A_Lens Schema Schema a b where
171− labelOptic = #paramSchema % #type
172− {-# INLINE labelOptic #-}
173−
174157instance
175158 ( a ~ Maybe SwaggerType
176159 , b ~ Maybe SwaggerType
177160 ) => LabelOptic "type" A_Lens NamedSchema NamedSchema a b where
178− labelOptic = #paramSchema % #type
161+ labelOptic = #schema % #type
179162 {-# INLINE labelOptic #-}
180163
181164-- #default
182165
183−instance
184− ( a ~ Maybe Value, b ~ Maybe Value
185− ) => LabelOptic "default" A_Lens Schema Schema a b where
186− labelOptic = #paramSchema % #default
187− {-# INLINE labelOptic #-}
188−
189166instance
190167 ( a ~ Maybe Value, b ~ Maybe Value
191168 ) => LabelOptic "default" A_Lens NamedSchema NamedSchema a b where
192− labelOptic = #paramSchema % #default
169+ labelOptic = #schema % #default
193170 {-# INLINE labelOptic #-}
194171
195172-- #format
196173
197−instance
198− ( a ~ Maybe Format, b ~ Maybe Format
199− ) => LabelOptic "format" A_Lens Schema Schema a b where
200− labelOptic = #paramSchema % #format
201− {-# INLINE labelOptic #-}
202−
203174instance
204175 ( a ~ Maybe Format, b ~ Maybe Format
205176 ) => LabelOptic "format" A_Lens NamedSchema NamedSchema a b where
206− labelOptic = #paramSchema % #format
177+ labelOptic = #schema % #format
207178 {-# INLINE labelOptic #-}
208179
209180-- #items
210181
211−instance
212− ( a ~ Maybe SwaggerItems
213− , b ~ Maybe SwaggerItems
214− ) => LabelOptic "items" A_Lens Schema Schema a b where
215− labelOptic = #paramSchema % #items
216− {-# INLINE labelOptic #-}
217−
218182instance
219183 ( a ~ Maybe SwaggerItems
220184 , b ~ Maybe SwaggerItems
221185 ) => LabelOptic "items" A_Lens NamedSchema NamedSchema a b where
222− labelOptic = #paramSchema % #items
186+ labelOptic = #schema % #items
223187 {-# INLINE labelOptic #-}
224188
225189-- #maximum
226190
227−instance
228− ( a ~ Maybe Scientific, b ~ Maybe Scientific
229− ) => LabelOptic "maximum" A_Lens Schema Schema a b where
230− labelOptic = #paramSchema % #maximum
231− {-# INLINE labelOptic #-}
232−
233191instance
234192 ( a ~ Maybe Scientific, b ~ Maybe Scientific
235193 ) => LabelOptic "maximum" A_Lens NamedSchema NamedSchema a b where
236− labelOptic = #paramSchema % #maximum
194+ labelOptic = #schema % #maximum
237195 {-# INLINE labelOptic #-}
238196
239197-- #exclusiveMaximum
240198
241−instance
242− ( a ~ Maybe Bool, b ~ Maybe Bool
243− ) => LabelOptic "exclusiveMaximum" A_Lens Schema Schema a b where
244− labelOptic = #paramSchema % #exclusiveMaximum
245− {-# INLINE labelOptic #-}
246−
247199instance
248200 ( a ~ Maybe Bool, b ~ Maybe Bool
249201 ) => LabelOptic "exclusiveMaximum" A_Lens NamedSchema NamedSchema a b where
250− labelOptic = #paramSchema % #exclusiveMaximum
202+ labelOptic = #schema % #exclusiveMaximum
251203 {-# INLINE labelOptic #-}
252204
253205-- #minimum
254206
255−instance
256− ( a ~ Maybe Scientific, b ~ Maybe Scientific
257− ) => LabelOptic "minimum" A_Lens Schema Schema a b where
258− labelOptic = #paramSchema % #minimum
259− {-# INLINE labelOptic #-}
260−
261207instance
262208 ( a ~ Maybe Scientific, b ~ Maybe Scientific
263209 ) => LabelOptic "minimum" A_Lens NamedSchema NamedSchema a b where
264− labelOptic = #paramSchema % #minimum
210+ labelOptic = #schema % #minimum
265211 {-# INLINE labelOptic #-}
266212
267213-- #exclusiveMinimum
268214
269−instance
270− ( a ~ Maybe Bool, b ~ Maybe Bool
271− ) => LabelOptic "exclusiveMinimum" A_Lens Schema Schema a b where
272− labelOptic = #paramSchema % #exclusiveMinimum
273− {-# INLINE labelOptic #-}
274−
275215instance
276216 ( a ~ Maybe Bool, b ~ Maybe Bool
277217 ) => LabelOptic "exclusiveMinimum" A_Lens NamedSchema NamedSchema a b where
278− labelOptic = #paramSchema % #exclusiveMinimum
218+ labelOptic = #schema % #exclusiveMinimum
279219 {-# INLINE labelOptic #-}
280220
281221-- #maxLength
282222
283−instance
284− ( a ~ Maybe Integer, b ~ Maybe Integer
285− ) => LabelOptic "maxLength" A_Lens Schema Schema a b where
286− labelOptic = #paramSchema % #maxLength
287− {-# INLINE labelOptic #-}
288−
289223instance
290224 ( a ~ Maybe Integer, b ~ Maybe Integer
291225 ) => LabelOptic "maxLength" A_Lens NamedSchema NamedSchema a b where
292− labelOptic = #paramSchema % #maxLength
226+ labelOptic = #schema % #maxLength
293227 {-# INLINE labelOptic #-}
294228
295229-- #minLength
296230
297−instance
298− ( a ~ Maybe Integer, b ~ Maybe Integer
299− ) => LabelOptic "minLength" A_Lens Schema Schema a b where
300− labelOptic = #paramSchema % #minLength
301− {-# INLINE labelOptic #-}
302−
303231instance
304232 ( a ~ Maybe Integer, b ~ Maybe Integer
305233 ) => LabelOptic "minLength" A_Lens NamedSchema NamedSchema a b where
306− labelOptic = #paramSchema % #minLength
234+ labelOptic = #schema % #minLength
307235 {-# INLINE labelOptic #-}
308236
309237-- #pattern
310238
311−instance
312− ( a ~ Maybe Text, b ~ Maybe Text
313− ) => LabelOptic "pattern" A_Lens Schema Schema a b where
314− labelOptic = #paramSchema % #pattern
315− {-# INLINE labelOptic #-}
316−
317239instance
318240 ( a ~ Maybe Text, b ~ Maybe Text
319241 ) => LabelOptic "pattern" A_Lens NamedSchema NamedSchema a b where
320− labelOptic = #paramSchema % #pattern
242+ labelOptic = #schema % #pattern
321243 {-# INLINE labelOptic #-}
322244
323245-- #maxItems
324246
325−instance
326− ( a ~ Maybe Integer, b ~ Maybe Integer
327− ) => LabelOptic "maxItems" A_Lens Schema Schema a b where
328− labelOptic = #paramSchema % #maxItems
329− {-# INLINE labelOptic #-}
330−
331247instance
332248 ( a ~ Maybe Integer, b ~ Maybe Integer
333249 ) => LabelOptic "maxItems" A_Lens NamedSchema NamedSchema a b where
334− labelOptic = #paramSchema % #maxItems
250+ labelOptic = #schema % #maxItems
335251 {-# INLINE labelOptic #-}
336252
337253-- #minItems
338254
339−instance
340− ( a ~ Maybe Integer, b ~ Maybe Integer
341− ) => LabelOptic "minItems" A_Lens Schema Schema a b where
342− labelOptic = #paramSchema % #minItems
343− {-# INLINE labelOptic #-}
344−
345255instance
346256 ( a ~ Maybe Integer, b ~ Maybe Integer
347257 ) => LabelOptic "minItems" A_Lens NamedSchema NamedSchema a b where
348− labelOptic = #paramSchema % #minItems
258+ labelOptic = #schema % #minItems
349259 {-# INLINE labelOptic #-}
350260
351261-- #uniqueItems
352262
353−instance
354− ( a ~ Maybe Bool, b ~ Maybe Bool
355− ) => LabelOptic "uniqueItems" A_Lens Schema Schema a b where
356− labelOptic = #paramSchema % #uniqueItems
357− {-# INLINE labelOptic #-}
358−
359263instance
360264 ( a ~ Maybe Bool, b ~ Maybe Bool
361265 ) => LabelOptic "uniqueItems" A_Lens NamedSchema NamedSchema a b where
362− labelOptic = #paramSchema % #uniqueItems
266+ labelOptic = #schema % #uniqueItems
363267 {-# INLINE labelOptic #-}
364268
365269-- #enum
366270
367−instance
368− ( a ~ Maybe [Value], b ~ Maybe [Value]
369− ) => LabelOptic "enum" A_Lens Schema Schema a b where
370− labelOptic = #paramSchema % #enum
371− {-# INLINE labelOptic #-}
372−
373271instance
374272 ( a ~ Maybe [Value], b ~ Maybe [Value]
375273 ) => LabelOptic "enum" A_Lens NamedSchema NamedSchema a b where
376− labelOptic = #paramSchema % #enum
274+ labelOptic = #schema % #enum
377275 {-# INLINE labelOptic #-}
378276
379277-- #multipleOf
380278
381−instance
382− ( a ~ Maybe Scientific, b ~ Maybe Scientific
383− ) => LabelOptic "multipleOf" A_Lens Schema Schema a b where
384− labelOptic = #paramSchema % #multipleOf
385− {-# INLINE labelOptic #-}
386−
387279instance
388280 ( a ~ Maybe Scientific, b ~ Maybe Scientific
389281 ) => LabelOptic "multipleOf" A_Lens NamedSchema NamedSchema a b where
390− labelOptic = #paramSchema % #multipleOf
282+ labelOptic = #schema % #multipleOf
391283 {-# INLINE labelOptic #-}
src/Data/Swagger/ParamSchema.hs+3−3
@@ -13,9 +13,9 @@ module Data.Swagger.ParamSchema (
1313 toParamSchemaBoundedIntegral,
1414
1515 -- * Schema templates
16− passwordParamSchema,
17− binaryParamSchema,
18− byteParamSchema,
16+ passwordSchema,
17+ binarySchema,
18+ byteSchema,
1919
2020 -- * Generic encoding configuration
2121 SchemaOptions(..),
src/Data/Swagger/Schema.hs−5
@@ -32,11 +32,6 @@ module Data.Swagger.Schema (
3232 paramSchemaToNamedSchema,
3333 paramSchemaToSchema,
3434
35− -- * Schema templates
36− passwordSchema,
37− binarySchema,
38− byteSchema,
39−
4035 -- * Sketching @'Schema'@s using @'ToJSON'@
4136 sketchSchema,
4237 sketchStrictSchema,
src/Data/Swagger/Schema/Generator.hs+1−1
@@ -29,7 +29,7 @@ import Test.QuickCheck.Property
2929-- and cannot be inferred.
3030schemaGen :: Definitions Schema -> Schema -> Gen Value
3131schemaGen _ schema
32− | Just cases <- schema ^. paramSchema . enum_ = elements cases
32+ | Just cases <- schema ^. enum_ = elements cases
3333schemaGen defns schema
3434 | Just variants <- schema ^. oneOf = schemaGen defns =<< elements (dereference defns <$> variants)
3535schemaGen defns schema =
test/Data/Swagger/ParamSchemaSpec.hs+1−1
@@ -22,7 +22,7 @@ import Data.Time.LocalTime
2222import qualified Data.HashMap.Strict as HM
2323
2424checkToParamSchema :: ToParamSchema a => Proxy a -> Value -> Spec
25−checkToParamSchema proxy js = (toParamSchema proxy :: ParamSchema) <=> js
25+checkToParamSchema proxy js = (toParamSchema proxy :: Schema) <=> js
2626
2727spec :: Spec
2828spec = do