Skip to content

Commit 4524cce

Browse files
authored
Merge pull request #35 from biocad/maksbotan/with-description-unique
version 0.1.3.15 WithDescription unique for fields
2 parents 185f592 + b155954 commit 4524cce

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.1.3.15] - 2023-11-20
10+
### Changed
11+
- `WithDescription` does not clash between different fields with the same type.
12+
913
## [0.1.3.14] - 2023-06-14
1014
### Added
1115
- Debug log formatter `debugLogHandler`, customizable log middleware `logMiddlewareCustom`.

cabal.project

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ source-repository-package
88
type: git
99
location: https://github.com/biocad/bcd-log
1010
tag: 16808faa4db0d3d94ff4006c5ca88691551ebe40
11+
--sha256: E/VW3Xb7Nvefle2QkTckuWn8QB2YhbBCEBVO2y/xcbo=
1112

1213
source-repository-package
1314
type: git

src/Web/Template/Servant/Swagger.hs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ module Web.Template.Servant.Swagger
99

1010
import Control.Lens (_Just, (%~), (&), (?~))
1111
import Data.Aeson (FromJSON, ToJSON, Value (..))
12-
import Data.OpenApi (ToSchema (..), defaultSchemaOptions, description, enum_,
13-
genericDeclareNamedSchema, schema)
12+
import Data.OpenApi (NamedSchema (..), Referenced (..), ToSchema (..),
13+
declareSchemaRef, defaultSchemaOptions, description, enum_,
14+
genericDeclareNamedSchema, oneOf, schema)
1415
import Data.OpenApi.Internal.Schema (GToSchema, rename)
1516
import Data.Override (Override)
1617
import Data.Override.Aeson ()
@@ -47,8 +48,24 @@ newtype WithDescription (descr :: Symbol) a
4748

4849
instance (Typeable a, KnownSymbol descr, ToSchema a) => ToSchema (WithDescription descr a) where
4950
declareNamedSchema _ = do
50-
sch <- declareNamedSchema @a Proxy
51-
return $ sch & schema . description ?~ pack (symbolVal @descr Proxy)
51+
-- Different fields may use the same type with different descriptions.
52+
-- To support this we use a hack: generate schema for a type, and
53+
-- wrap it for specific field with 'oneOf' with exactly one option, adding
54+
-- concrete description.
55+
--
56+
-- Schema for a field with description should be unnamed, to prevent it
57+
-- from reusage in other fields.
58+
--
59+
-- OpenAPI 3.1 will have a mechanism for this without hacks, but we use 3.0.
60+
sch <- declareSchemaRef @a Proxy
61+
case sch of
62+
-- If schema for a field is something simple like "type: string",
63+
-- add description directly to it. It won't be shared with other fields.
64+
Inline sub -> return $ NamedSchema Nothing $ sub
65+
& description ?~ pack (symbolVal @descr Proxy)
66+
Ref ref -> return $ NamedSchema Nothing $ mempty
67+
& description ?~ pack (symbolVal @descr Proxy)
68+
& oneOf ?~ [Ref ref]
5269

5370
-- | Describe possible enumeration values for a 'Text' field.
5471
--

web-template.cabal

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: web-template
2-
version: 0.1.3.14
2+
version: 0.1.3.15
33
synopsis: Web template
44
description:
55
Web template includes:
@@ -67,7 +67,8 @@ library
6767
, openapi3 >= 3.2.0
6868
, openid-connect >= 0.1.1
6969
, resourcet
70-
, scotty
70+
-- 0.20 has breaking changes
71+
, scotty < 0.20
7172
, servant >= 0.18
7273
, servant-openapi3
7374
, servant-server >= 0.18

0 commit comments

Comments
 (0)