Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that query param types are not inlined in OpenAPI spec #861

Merged
merged 3 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions primer-service/primer-service.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ library
, http-client ^>=0.7.13
, http-media >=0.8 && <0.9.0
, http-types ^>=0.12.3
, insert-ordered-containers ^>=0.2.5.1
, logging-effect ^>=1.3.13
, mtl >=2.2.2 && <2.4.0
, openapi3 >=3.2 && <3.3.0
Expand Down
42 changes: 40 additions & 2 deletions primer-service/src/Primer/Server.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DisambiguateRecordFields #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedLabels #-}
Expand All @@ -17,7 +18,8 @@ import Control.Concurrent.STM (

import Control.Monad.Log (LoggingT, WithSeverity, runLoggingT)
import Control.Monad.Log qualified as Log
import Data.OpenApi (OpenApi)
import Data.HashMap.Strict.InsOrd qualified as IOHM
import Data.OpenApi (OpenApi, Reference (Reference), Referenced (Inline, Ref), ToSchema, toSchema)
import Data.Streaming.Network.Internal (HostPreference (HostIPv4Only))
import Data.Text qualified as T
import Data.Text.Lazy qualified as LT (fromStrict)
Expand All @@ -44,7 +46,7 @@ import Network.Wai.Middleware.Cors (
simpleMethods,
)
import Network.Wai.Middleware.Prometheus qualified as P
import Optics ((%), (.~), (?~))
import Optics (mapped, (%), (%~), (.~), (?~), (^.))
import Primer.API (
APILog,
Env (..),
Expand All @@ -65,6 +67,8 @@ import Primer.API (
runPrimerM,
)
import Primer.API qualified as API
import Primer.Action.Available (InputAction, NoInputAction)
import Primer.App (Level)
import Primer.Core (globalNamePretty, moduleNamePretty, qualifyName)
import Primer.Database (
SessionId,
Expand Down Expand Up @@ -93,6 +97,7 @@ import Servant (
import Servant.API.Generic (GenericMode ((:-)))
import Servant.OpenApi (toOpenApi)
import Servant.Server.Generic (AsServerT, genericServeT)
import Type.Reflection (typeRep)

type Primer l = (PrimerM (LoggingT (WithSeverity l) IO))

Expand All @@ -107,6 +112,39 @@ openAPIInfo =
& #info % #title .~ "Primer backend API"
& #info % #description ?~ "A backend service implementing a pedagogic functional programming language."
& #info % #version .~ "0.7"
& refParamSchemas @Level
[ ("/openapi/sessions/{sessionId}/action/available", "level")
, ("/openapi/sessions/{sessionId}/action/options", "level")
]
& refParamSchemas @InputAction
[ ("/openapi/sessions/{sessionId}/action/apply/input", "action")
]
& refParamSchemas @NoInputAction
[ ("/openapi/sessions/{sessionId}/action/apply/simple", "action")
]
where
{- This is a workaround for an upstream issue: https://github.com/biocad/servant-openapi3/issues/37.
Given a type, and some query parameters of that type,
this ensures that the specification of each parameter references a common schema,
instead of inlining it.
This could be made more general, (visit non-POST endpoints,
modify `show . typeRep` output when it contains ` ` to match `openapi3`,
search by schema shape so that we don't have to manually enumerate use sites),
but _hopefully_ this will just be fixed upstream before we have to worry about any of that.
-}
refParamSchemas :: forall a. ToSchema a => [(FilePath, Text)] -> OpenApi -> OpenApi
refParamSchemas params api =
api
& #components % #schemas %~ IOHM.insert name (toSchema $ Proxy @a)
& #paths %~ composeList (map (uncurry $ flip adjustParam) params)
where
composeList = appEndo . foldMap' Endo
adjustParam paramName =
IOHM.adjust $
#post % mapped % #parameters % mapped %~ \case
Inline x | x ^. #name == paramName -> Inline $ x & #schema ?~ Ref (Reference name)
p -> p
name = show $ typeRep @a

openAPIServer :: ConvertServerLogs l => OpenAPI.RootAPI (AsServerT (Primer l))
openAPIServer =
Expand Down
66 changes: 12 additions & 54 deletions primer-service/test/outputs/OpenAPI/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@
"format": "date-time",
"type": "string"
},
"Level": {
"enum": [
"Beginner",
"Intermediate",
"Expert"
],
"type": "string"
},
"Module": {
"properties": {
"defs": {
Expand Down Expand Up @@ -830,28 +838,7 @@
"name": "action",
"required": true,
"schema": {
"enum": [
"MakeCon",
"MakeConSat",
"MakeInt",
"MakeChar",
"MakeVar",
"MakeVarSat",
"MakeLet",
"MakeLetRec",
"MakeLam",
"MakeLAM",
"RenamePattern",
"RenameLet",
"RenameLam",
"RenameLAM",
"MakeTCon",
"MakeTVar",
"MakeForall",
"RenameForall",
"RenameDef"
],
"type": "string"
"$ref": "#/components/schemas/InputAction"
}
}
],
Expand Down Expand Up @@ -913,26 +900,7 @@
"name": "action",
"required": true,
"schema": {
"enum": [
"MakeCase",
"MakeApp",
"MakeAPP",
"MakeAnn",
"RemoveAnn",
"LetToRec",
"Raise",
"EnterHole",
"RemoveHole",
"DeleteExpr",
"MakeFun",
"AddInput",
"MakeTApp",
"RaiseType",
"DeleteType",
"DuplicateDef",
"DeleteDef"
],
"type": "string"
"$ref": "#/components/schemas/NoInputAction"
}
}
],
Expand Down Expand Up @@ -985,12 +953,7 @@
"name": "level",
"required": true,
"schema": {
"enum": [
"Beginner",
"Intermediate",
"Expert"
],
"type": "string"
"$ref": "#/components/schemas/Level"
}
}
],
Expand Down Expand Up @@ -1046,12 +1009,7 @@
"name": "level",
"required": true,
"schema": {
"enum": [
"Beginner",
"Intermediate",
"Expert"
],
"type": "string"
"$ref": "#/components/schemas/Level"
}
},
{
Expand Down