Skip to content
Open
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
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
5 changes: 5 additions & 0 deletions cli/src/TestGenScript.elm
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ 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")
Expand Down Expand Up @@ -143,6 +147,7 @@ run =
|> OpenApi.Config.withInput multipartFormData
|> 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 @@ -280,6 +280,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)
)
Comment thread
miniBill marked this conversation as resolved.


pathDeclarations : List OpenApi.Config.EffectType -> ServerInfo -> CliMonad (List CliMonad.Declaration)
pathDeclarations effectTypes server =
CliMonad.getApiSpec
Expand All @@ -301,7 +337,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 @@ -488,8 +524,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
step : OperationUtils -> CliMonad (List CliMonad.Declaration)
step ({ successType, bodyTypeAnnotation, errorTypeDeclaration, errorTypeAnnotation } as operationUtils) =
Expand Down Expand Up @@ -523,7 +568,7 @@ toRequestFunctions server effectTypes method pathUrl operation =
|> CliMonad.andThen
(\params ->
toConfigParamAnnotation
{ operation = operation
{ allParams = allParams
, successAnnotation = successAnnotation
, errorBodyAnnotation = bodyTypeAnnotation
, errorTypeAnnotation = errorTypeAnnotation
Expand All @@ -533,11 +578,11 @@ toRequestFunctions server effectTypes method pathUrl operation =
}
)
)
(replacedUrl server auth pathUrl operation)
(replacedUrl server auth pathUrl allParams)
)
(operationToContentSchema operation)
(operationToAuthorizationInfo operation)
(operationToHeaderParams operation)
(operationToHeaderParams allParams)
(case successType of
SuccessType t ->
SchemaUtils.typeToAnnotationWithNullable t
Expand Down Expand Up @@ -1323,10 +1368,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 @@ -1372,8 +1416,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 @@ -1451,8 +1495,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 @@ -1965,7 +2008,7 @@ multipartContent mediaType =


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 @@ -2025,7 +2068,7 @@ toConfigParamAnnotation options =
, lamderaProgramTest = toMsgLamderaProgramTest
}
)
(operationToUrlParams options.operation)
(operationToUrlParams options.allParams)


type ServerInfo
Expand Down Expand Up @@ -2091,13 +2134,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
Loading