Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions cli/example/path-level-params.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
openapi: "3.1.0"
info:
title: "Path Level Params Test"
version: "1.0.0"
paths:
/orgs/{orgId}/teams/{teamId}/items:
parameters:
- $ref: '#/components/parameters/orgIdParam'
- in: path
name: teamId
required: true
schema:
type: string
get:
operationId: getItems
parameters:
- in: query
name: status
required: false
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
type: object
properties:
count:
type: integer
required:
- count
components:
parameters:
orgIdParam:
in: path
name: orgId
required: true
schema:
type: string
53 changes: 29 additions & 24 deletions cli/src/TestGenScript.elm
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,23 @@ run =
binaryResponse =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/binary-response.yaml")

bug : Int -> OpenApi.Config.Input
bug n =
OpenApi.Config.inputFrom (OpenApi.Config.File ("./example/openapi-generator-bugs/" ++ String.fromInt n ++ ".yaml"))

cookieAuth : OpenApi.Config.Input
cookieAuth =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/cookie-auth.yaml")

dbFahrplanApi : OpenApi.Config.Input
dbFahrplanApi =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/db-fahrplan-api-specification.yaml")

gitHub : OpenApi.Config.Input
gitHub =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/github-spec.json")
|> OpenApi.Config.withWarnOnMissingEnums False

ifconfigOvh : OpenApi.Config.Input
ifconfigOvh =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/ifconfig.ovh.json")
Expand All @@ -60,6 +73,14 @@ run =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/overriding-global-security.yaml")
|> OpenApi.Config.withOverrides [ OpenApi.Config.File "./example/overriding-global-security-override.yaml" ]

pathLevelParams : OpenApi.Config.Input
pathLevelParams =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/path-level-params.yaml")

patreon : OpenApi.Config.Input
patreon =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/patreon.json")

realworldConduit : OpenApi.Config.Input
realworldConduit =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/realworld-conduit.yaml")
Expand All @@ -76,9 +97,9 @@ run =
singleEnum =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/single-enum.yaml")

uuidArrayParam : OpenApi.Config.Input
uuidArrayParam =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/uuid-array-param.yaml")
telegramBot : OpenApi.Config.Input
telegramBot =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/telegram-bot.json")

trustmark : OpenApi.Config.Input
trustmark =
Expand All @@ -93,31 +114,14 @@ run =
|> OpenApi.Config.withOutputModuleName [ "Trustmark", "TradeCheck" ]
|> OpenApi.Config.withEffectTypes [ OpenApi.Config.ElmHttpCmd ]

uuidArrayParam : OpenApi.Config.Input
uuidArrayParam =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/uuid-array-param.yaml")

viaggiatreno : OpenApi.Config.Input
viaggiatreno =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/viaggiatreno.yaml")

bug : Int -> OpenApi.Config.Input
bug n =
OpenApi.Config.inputFrom (OpenApi.Config.File ("./example/openapi-generator-bugs/" ++ String.fromInt n ++ ".yaml"))

dbFahrplanApi : OpenApi.Config.Input
dbFahrplanApi =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/db-fahrplan-api-specification.yaml")

gitHub : OpenApi.Config.Input
gitHub =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/github-spec.json")
|> OpenApi.Config.withWarnOnMissingEnums False

patreon : OpenApi.Config.Input
patreon =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/patreon.json")

telegramBot : OpenApi.Config.Input
telegramBot =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/telegram-bot.json")

profileConfig : OpenApi.Config.Config
profileConfig =
-- Slimmed config for profiling
Expand All @@ -133,6 +137,7 @@ run =
|> OpenApi.Config.withInput marioPartyStats
|> OpenApi.Config.withInput nullableEnum
|> OpenApi.Config.withInput overridingGlobalSecurity
|> OpenApi.Config.withInput pathLevelParams
|> OpenApi.Config.withInput realworldConduit
|> OpenApi.Config.withInput recursiveAllOfRefs
|> OpenApi.Config.withInput simpleRef
Expand Down
84 changes: 61 additions & 23 deletions src/OpenApi/Generate.elm
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,42 @@ stripTrailingSlash input =
input


{-| Merge path-level parameters with operation-level parameters.
Per the OpenAPI spec, operation-level parameters override path-level
parameters with the same name and location.
-}
mergeParams :
List (OpenApi.Reference.ReferenceOr OpenApi.Parameter.Parameter)
-> List (OpenApi.Reference.ReferenceOr OpenApi.Parameter.Parameter)
-> CliMonad (List (OpenApi.Reference.ReferenceOr OpenApi.Parameter.Parameter))
mergeParams pathParams operationParams =
let
paramKey : OpenApi.Reference.ReferenceOr OpenApi.Parameter.Parameter -> CliMonad String
paramKey param =
toConcreteParam param
|> CliMonad.map (\concrete -> OpenApi.Parameter.in_ concrete ++ ":" ++ OpenApi.Parameter.name concrete)
in
CliMonad.combineMap paramKey operationParams
|> CliMonad.map FastSet.fromList
|> CliMonad.andThen
(\operationParamKeys ->
pathParams
|> CliMonad.combineMap
(\param ->
paramKey param
|> CliMonad.map
(\key ->
if FastSet.member key operationParamKeys then
Nothing

else
Just param
)
)
|> CliMonad.map (\filtered -> List.filterMap identity filtered ++ operationParams)
)


pathDeclarations : List OpenApi.Config.EffectType -> ServerInfo -> CliMonad (List CliMonad.Declaration)
pathDeclarations effectTypes server =
CliMonad.getApiSpec
Expand All @@ -294,7 +330,7 @@ pathDeclarations effectTypes server =
|> List.filterMap (\( method, getter ) -> Maybe.map (Tuple.pair method) (getter path))
|> CliMonad.combineMap
(\( method, operation ) ->
toRequestFunctions server effectTypes method url operation
toRequestFunctions server effectTypes method url (OpenApi.Path.parameters path) operation
|> CliMonad.errorToWarning
)
|> CliMonad.map (List.filterMap identity >> List.concat)
Expand Down Expand Up @@ -462,8 +498,17 @@ requestBodyToDeclarations name reference =
|> CliMonad.withPath name


toRequestFunctions : ServerInfo -> List OpenApi.Config.EffectType -> String -> String -> OpenApi.Operation.Operation -> CliMonad (List CliMonad.Declaration)
toRequestFunctions server effectTypes method pathUrl operation =
toRequestFunctions : ServerInfo -> List OpenApi.Config.EffectType -> String -> String -> List (OpenApi.Reference.ReferenceOr OpenApi.Parameter.Parameter) -> OpenApi.Operation.Operation -> CliMonad (List CliMonad.Declaration)
toRequestFunctions server effectTypes method pathUrl pathLevelParams operation =
mergeParams pathLevelParams (OpenApi.Operation.parameters operation)
|> CliMonad.andThen
(\allParams ->
toRequestFunctionsHelp server effectTypes method pathUrl operation allParams
)


toRequestFunctionsHelp : ServerInfo -> List OpenApi.Config.EffectType -> String -> String -> OpenApi.Operation.Operation -> List (OpenApi.Reference.ReferenceOr OpenApi.Parameter.Parameter) -> CliMonad (List CliMonad.Declaration)
toRequestFunctionsHelp server effectTypes method pathUrl operation allParams =
let
functionName : String
functionName =
Expand Down Expand Up @@ -1142,7 +1187,7 @@ toRequestFunctions server effectTypes method pathUrl operation =
|> CliMonad.andThen
(\params ->
toConfigParamAnnotation
{ operation = operation
{ allParams = allParams
, successAnnotation = successAnnotation
, errorBodyAnnotation = bodyTypeAnnotation
, errorTypeAnnotation = errorTypeAnnotation
Expand All @@ -1152,8 +1197,8 @@ toRequestFunctions server effectTypes method pathUrl operation =
}
)
)
(replacedUrl server auth pathUrl operation)
(operationToHeaderParams operation)
(replacedUrl server auth pathUrl allParams)
(operationToHeaderParams allParams)
)
(operationToContentSchema operation)
(operationToAuthorizationInfo operation)
Expand Down Expand Up @@ -1181,10 +1226,9 @@ operationToGroup operation =
"Operations"


operationToHeaderParams : OpenApi.Operation.Operation -> CliMonad (List (Elm.Expression -> ( Elm.Expression, Elm.Expression, Bool )))
operationToHeaderParams operation =
operation
|> OpenApi.Operation.parameters
operationToHeaderParams : List (OpenApi.Reference.ReferenceOr OpenApi.Parameter.Parameter) -> CliMonad (List (Elm.Expression -> ( Elm.Expression, Elm.Expression, Bool )))
operationToHeaderParams params =
params
|> CliMonad.combineMap
(\param ->
toConcreteParam param
Expand Down Expand Up @@ -1230,8 +1274,8 @@ operationToHeaderParams operation =
|> CliMonad.map (List.filterMap identity)


replacedUrl : ServerInfo -> AuthorizationInfo -> String -> OpenApi.Operation.Operation -> CliMonad (Elm.Expression -> Elm.Expression)
replacedUrl server authInfo pathUrl operation =
replacedUrl : ServerInfo -> AuthorizationInfo -> String -> List (OpenApi.Reference.ReferenceOr OpenApi.Parameter.Parameter) -> CliMonad (Elm.Expression -> Elm.Expression)
replacedUrl server authInfo pathUrl params =
let
pathSegments : List String
pathSegments =
Expand Down Expand Up @@ -1309,8 +1353,7 @@ replacedUrl server authInfo pathUrl operation =
MultipleServers _ ->
Gen.Url.Builder.call_.crossOrigin (Elm.get "server" config) (Elm.list replacedSegments) allQueryParams
in
operation
|> OpenApi.Operation.parameters
params
|> CliMonad.combineMap
(\param ->
toConcreteParam param
Expand Down Expand Up @@ -1761,7 +1804,7 @@ contentToContentSchema content =


toConfigParamAnnotation :
{ operation : OpenApi.Operation.Operation
{ allParams : List (OpenApi.Reference.ReferenceOr OpenApi.Parameter.Parameter)
, successAnnotation : Elm.Annotation.Annotation
, errorBodyAnnotation : Elm.Annotation.Annotation
, errorTypeAnnotation : Elm.Annotation.Annotation
Expand Down Expand Up @@ -1820,7 +1863,7 @@ toConfigParamAnnotation options =
, lamderaProgramTest = toAnnotation toMsgLamderaProgramTest
}
)
(operationToUrlParams options.operation)
(operationToUrlParams options.allParams)


type ServerInfo
Expand Down Expand Up @@ -1886,13 +1929,8 @@ serverInfo server =
|> CliMonad.succeed


operationToUrlParams : OpenApi.Operation.Operation -> CliMonad (List ( Common.UnsafeName, Elm.Annotation.Annotation ))
operationToUrlParams operation =
let
params : List (OpenApi.Reference.ReferenceOr OpenApi.Parameter.Parameter)
params =
OpenApi.Operation.parameters operation
in
operationToUrlParams : List (OpenApi.Reference.ReferenceOr OpenApi.Parameter.Parameter) -> CliMonad (List ( Common.UnsafeName, Elm.Annotation.Annotation ))
operationToUrlParams params =
if List.isEmpty params then
CliMonad.succeed []

Expand Down
Loading