Skip to content

Commit 2e3a6ec

Browse files
lepsaelland
andauthored
WPB-4240: Migrate from swagger2 to openapi3 (#3570)
--------- Co-authored-by: Igor Ranieri Elland <[email protected]> Co-authored-by: Igor Ranieri <[email protected]>
1 parent 85cb5a9 commit 2e3a6ec

File tree

191 files changed

+890
-579
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

191 files changed

+890
-579
lines changed

changelog.d/4-docs/WPB-4240

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Updating the route documentation from Swagger 2 to OpenAPI 3.

changelog.d/5-internal/WPB-4240

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Updating the route documentation library from swagger2 to openapi3.
2+
3+
This also introduced a breaking change in how we track what federation calls each route makes.
4+
The openapi3 library doesn't support extension fields, and as such tags are being used instead in a similar way.

libs/brig-types/brig-types.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ test-suite brig-types-tests
156156
, brig-types
157157
, bytestring-conversion >=0.3.1
158158
, imports
159+
, openapi3
159160
, QuickCheck >=2.9
160-
, swagger2 >=2.5
161161
, tasty
162162
, tasty-hunit
163163
, tasty-quickcheck

libs/brig-types/default.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
, gitignoreSource
1414
, imports
1515
, lib
16+
, openapi3
1617
, QuickCheck
17-
, swagger2
1818
, tasty
1919
, tasty-hunit
2020
, tasty-quickcheck
@@ -47,8 +47,8 @@ mkDerivation {
4747
base
4848
bytestring-conversion
4949
imports
50+
openapi3
5051
QuickCheck
51-
swagger2
5252
tasty
5353
tasty-hunit
5454
tasty-quickcheck

libs/brig-types/test/unit/Test/Brig/Roundtrip.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Test.Brig.Roundtrip where
2020
import Data.Aeson (FromJSON, ToJSON, parseJSON, toJSON)
2121
import Data.Aeson.Types (parseEither)
2222
import Data.ByteString.Conversion
23-
import Data.Swagger (ToSchema, validatePrettyToJSON)
23+
import Data.OpenApi (ToSchema, validatePrettyToJSON)
2424
import Imports
2525
import Test.Tasty (TestTree)
2626
import Test.Tasty.QuickCheck (Arbitrary, counterexample, testProperty, (.&&.), (===))
@@ -40,7 +40,7 @@ testRoundTrip = testProperty msg trip
4040

4141
testRoundTripWithSwagger ::
4242
forall a.
43-
(Arbitrary a, Typeable a, ToJSON a, FromJSON a, ToSchema a, Eq a, Show a) =>
43+
(Arbitrary a, ToJSON a, FromJSON a, ToSchema a, Eq a, Show a) =>
4444
TestTree
4545
testRoundTripWithSwagger = testProperty msg (trip .&&. scm)
4646
where

libs/deriving-swagger2/default.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
, gitignoreSource
99
, imports
1010
, lib
11-
, swagger2
11+
, openapi3
1212
}:
1313
mkDerivation {
1414
pname = "deriving-swagger2";
1515
version = "0.1.0";
1616
src = gitignoreSource ./.;
17-
libraryHaskellDepends = [ base extra imports swagger2 ];
17+
libraryHaskellDepends = [ base extra imports openapi3 ];
1818
license = lib.licenses.agpl3Only;
1919
}

libs/deriving-swagger2/deriving-swagger2.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ library
6262
-Wredundant-constraints -Wunused-packages
6363

6464
build-depends:
65-
base >=4 && <5
65+
base >=4 && <5
6666
, extra
6767
, imports
68-
, swagger2 >=0.6
68+
, openapi3
6969

7070
default-language: GHC2021

libs/deriving-swagger2/src/Deriving/Swagger.hs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE RankNTypes #-}
2+
13
-- This file is part of the Wire Server implementation.
24
--
35
-- Copyright (C) 2022 Wire Swiss GmbH <[email protected]>
@@ -22,10 +24,10 @@ module Deriving.Swagger where
2224
import Data.Char qualified as Char
2325
import Data.Kind (Constraint)
2426
import Data.List.Extra (stripSuffix)
27+
import Data.OpenApi.Internal.Schema (GToSchema)
28+
import Data.OpenApi.Internal.TypeShape
29+
import Data.OpenApi.Schema
2530
import Data.Proxy (Proxy (..))
26-
import Data.Swagger (SchemaOptions, ToSchema (..), constructorTagModifier, defaultSchemaOptions, fieldLabelModifier, genericDeclareNamedSchema)
27-
import Data.Swagger.Internal.Schema (GToSchema)
28-
import Data.Swagger.Internal.TypeShape (TypeHasSimpleShape)
2931
import GHC.Generics (Generic (Rep))
3032
import GHC.TypeLits (ErrorMessage (Text), KnownSymbol, Symbol, TypeError, symbolVal)
3133
import Imports
@@ -81,6 +83,7 @@ import Imports
8183
-- | A newtype wrapper which gives ToSchema instances with modified options.
8284
-- 't' has to have an instance of the 'SwaggerOptions' class.
8385
newtype CustomSwagger t a = CustomSwagger {unCustomSwagger :: a}
86+
deriving (Generic, Typeable)
8487

8588
class SwaggerOptions xs where
8689
swaggerOptions :: SchemaOptions
@@ -94,14 +97,7 @@ instance (StringModifier f, SwaggerOptions xs) => SwaggerOptions (FieldLabelModi
9497
instance (StringModifier f, SwaggerOptions xs) => SwaggerOptions (ConstructorTagModifier f ': xs) where
9598
swaggerOptions = (swaggerOptions @xs) {constructorTagModifier = getStringModifier @f}
9699

97-
instance
98-
( SwaggerOptions t,
99-
Generic a,
100-
GToSchema (Rep a),
101-
TypeHasSimpleShape a "genericDeclareNamedSchemaUnrestricted"
102-
) =>
103-
ToSchema (CustomSwagger t a)
104-
where
100+
instance (SwaggerOptions t, Generic a, Typeable a, GToSchema (Rep a), Typeable (CustomSwagger t a), TypeHasSimpleShape a "genericDeclareNamedSchemaUnrestricted") => ToSchema (CustomSwagger t a) where
105101
declareNamedSchema _ = genericDeclareNamedSchema (swaggerOptions @t) (Proxy @a)
106102

107103
-- ** Specify __what__ to modify

libs/extended/default.nix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
, servant
2828
, servant-client
2929
, servant-client-core
30+
, servant-openapi3
3031
, servant-server
31-
, servant-swagger
3232
, temporary
3333
, text
3434
, tinylog
@@ -60,8 +60,8 @@ mkDerivation {
6060
servant
6161
servant-client
6262
servant-client-core
63+
servant-openapi3
6364
servant-server
64-
servant-swagger
6565
text
6666
tinylog
6767
unliftio

libs/extended/extended.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ library
9898
, servant
9999
, servant-client
100100
, servant-client-core
101+
, servant-openapi3
101102
, servant-server
102-
, servant-swagger
103103
, text
104104
, tinylog
105105
, unliftio

libs/extended/src/Servant/API/Extended.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ import Network.Wai
3131
import Servant.API
3232
import Servant.API.ContentTypes
3333
import Servant.API.Modifiers
34+
import Servant.OpenApi
3435
import Servant.Server.Internal
35-
import Servant.Swagger
3636
import Prelude ()
3737

3838
-- | Like 'ReqBody'', but takes parsers that throw 'ServerError', not 'String'. @tag@ is used
@@ -108,10 +108,10 @@ instance
108108
Right v -> pure v
109109

110110
instance
111-
HasSwagger (ReqBody' '[Required, Strict] cts a :> api) =>
112-
HasSwagger (ReqBodyCustomError cts tag a :> api)
111+
HasOpenApi (ReqBody' '[Required, Strict] cts a :> api) =>
112+
HasOpenApi (ReqBodyCustomError cts tag a :> api)
113113
where
114-
toSwagger Proxy = toSwagger (Proxy @(ReqBody' '[Required, Strict] cts a :> api))
114+
toOpenApi Proxy = toOpenApi (Proxy @(ReqBody' '[Required, Strict] cts a :> api))
115115

116116
instance RoutesToPaths rest => RoutesToPaths (ReqBodyCustomError' mods list tag a :> rest) where
117117
getRoutes = getRoutes @rest

libs/extended/src/Servant/API/Extended/RawM.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import Data.Proxy
1010
import Imports
1111
import Network.Wai
1212
import Servant.API (Raw)
13+
import Servant.OpenApi
1314
import Servant.Server hiding (respond)
1415
import Servant.Server.Internal.Delayed
1516
import Servant.Server.Internal.RouteResult
1617
import Servant.Server.Internal.Router
17-
import Servant.Swagger
1818

1919
type ApplicationM m = Request -> (Response -> IO ResponseReceived) -> m ResponseReceived
2020

@@ -51,8 +51,8 @@ instance HasServer RawM context where
5151

5252
hoistServerWithContext _ _ f srvM req respond = f (srvM req respond)
5353

54-
instance HasSwagger RawM where
55-
toSwagger _ = toSwagger (Proxy @Raw)
54+
instance HasOpenApi RawM where
55+
toOpenApi _ = toOpenApi (Proxy @Raw)
5656

5757
instance RoutesToPaths RawM where
5858
getRoutes = []

libs/schema-profunctor/default.nix

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
, insert-ordered-containers
1515
, lens
1616
, lib
17+
, openapi3
1718
, profunctors
18-
, swagger2
1919
, tasty
2020
, tasty-hunit
2121
, text
@@ -34,8 +34,8 @@ mkDerivation {
3434
containers
3535
imports
3636
lens
37+
openapi3
3738
profunctors
38-
swagger2
3939
text
4040
transformers
4141
vector
@@ -47,7 +47,7 @@ mkDerivation {
4747
imports
4848
insert-ordered-containers
4949
lens
50-
swagger2
50+
openapi3
5151
tasty
5252
tasty-hunit
5353
text

libs/schema-profunctor/schema-profunctor.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ library
6969
, containers
7070
, imports
7171
, lens
72+
, openapi3
7273
, profunctors
73-
, swagger2 >=2 && <2.9
7474
, text
7575
, transformers
7676
, vector
@@ -139,8 +139,8 @@ test-suite schemas-tests
139139
, imports
140140
, insert-ordered-containers
141141
, lens
142+
, openapi3
142143
, schema-profunctor
143-
, swagger2
144144
, tasty
145145
, tasty-hunit
146146
, text

libs/schema-profunctor/src/Data/Schema.hs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,11 @@ import Data.List.NonEmpty (NonEmpty)
100100
import Data.List.NonEmpty qualified as NonEmpty
101101
import Data.Map qualified as Map
102102
import Data.Monoid hiding (Product)
103+
import Data.OpenApi qualified as S
104+
import Data.OpenApi.Declare qualified as S
103105
import Data.Profunctor (Star (..))
104106
import Data.Proxy (Proxy (..))
105107
import Data.Set qualified as Set
106-
import Data.Swagger qualified as S
107-
import Data.Swagger.Declare qualified as S
108-
import Data.Swagger.Internal qualified as S
109108
import Data.Text qualified as T
110109
import Data.Text.Lazy qualified as TL
111110
import Data.Vector qualified as V
@@ -624,7 +623,7 @@ text name =
624623
(A.withText (T.unpack name) pure)
625624
(pure . A.String)
626625
where
627-
d = mempty & S.type_ ?~ S.SwaggerString
626+
d = mempty & S.type_ ?~ S.OpenApiString
628627

629628
-- | A schema for a textual value with possible failure.
630629
parsedText ::
@@ -764,7 +763,7 @@ instance HasSchemaRef doc => HasField doc SwaggerDoc where
764763
where
765764
f ref =
766765
mempty
767-
& S.type_ ?~ S.SwaggerObject
766+
& S.type_ ?~ S.OpenApiObject
768767
& S.properties . at name ?~ ref
769768
& S.required .~ [name]
770769

@@ -780,16 +779,16 @@ instance HasSchemaRef ndoc => HasArray ndoc SwaggerDoc where
780779
f :: S.Referenced S.Schema -> S.Schema
781780
f ref =
782781
mempty
783-
& S.type_ ?~ S.SwaggerArray
784-
& S.items ?~ S.SwaggerItemsObject ref
782+
& S.type_ ?~ S.OpenApiArray
783+
& S.items ?~ S.OpenApiItemsObject ref
785784

786785
instance HasSchemaRef ndoc => HasMap ndoc SwaggerDoc where
787786
mkMap = fmap f . schemaRef
788787
where
789788
f :: S.Referenced S.Schema -> S.Schema
790789
f ref =
791790
mempty
792-
& S.type_ ?~ S.SwaggerObject
791+
& S.type_ ?~ S.OpenApiObject
793792
& S.additionalProperties ?~ S.AdditionalPropertiesSchema ref
794793

795794
class HasMinItems s a where
@@ -799,19 +798,19 @@ instance HasMinItems SwaggerDoc (Maybe Integer) where
799798
minItems = declared . S.minItems
800799

801800
instance HasEnum Text NamedSwaggerDoc where
802-
mkEnum = mkSwaggerEnum S.SwaggerString
801+
mkEnum = mkSwaggerEnum S.OpenApiString
803802

804803
instance HasEnum Integer NamedSwaggerDoc where
805-
mkEnum = mkSwaggerEnum S.SwaggerInteger
804+
mkEnum = mkSwaggerEnum S.OpenApiInteger
806805

807806
instance HasEnum Natural NamedSwaggerDoc where
808-
mkEnum = mkSwaggerEnum S.SwaggerInteger
807+
mkEnum = mkSwaggerEnum S.OpenApiInteger
809808

810809
instance HasEnum Bool NamedSwaggerDoc where
811-
mkEnum = mkSwaggerEnum S.SwaggerBoolean
810+
mkEnum = mkSwaggerEnum S.OpenApiBoolean
812811

813812
mkSwaggerEnum ::
814-
S.SwaggerType 'S.SwaggerKindSchema ->
813+
S.OpenApiType ->
815814
Text ->
816815
[A.Value] ->
817816
NamedSwaggerDoc
@@ -839,11 +838,12 @@ class ToSchema a where
839838
-- Newtype wrappers for deriving via
840839

841840
newtype Schema a = Schema {getSchema :: a}
841+
deriving (Generic)
842842

843843
schemaToSwagger :: forall a. ToSchema a => Proxy a -> Declare S.NamedSchema
844844
schemaToSwagger _ = runDeclare (schemaDoc (schema @a))
845845

846-
instance ToSchema a => S.ToSchema (Schema a) where
846+
instance (Typeable a, ToSchema a) => S.ToSchema (Schema a) where
847847
declareNamedSchema _ = schemaToSwagger (Proxy @a)
848848

849849
-- | JSON serialiser for an instance of 'ToSchema'.
@@ -920,8 +920,14 @@ instance S.HasSchema d S.Schema => S.HasSchema (SchemaP d v w a b) S.Schema wher
920920
instance S.HasDescription NamedSwaggerDoc (Maybe Text) where
921921
description = declared . S.schema . S.description
922922

923+
instance S.HasDeprecated NamedSwaggerDoc (Maybe Bool) where
924+
deprecated = declared . S.schema . S.deprecated
925+
923926
instance {-# OVERLAPPABLE #-} S.HasDescription s a => S.HasDescription (WithDeclare s) a where
924927
description = declared . S.description
925928

929+
instance {-# OVERLAPPABLE #-} S.HasDeprecated s a => S.HasDeprecated (WithDeclare s) a where
930+
deprecated = declared . S.deprecated
931+
926932
instance {-# OVERLAPPABLE #-} S.HasExample s a => S.HasExample (WithDeclare s) a where
927933
example = declared . S.example

0 commit comments

Comments
 (0)