Skip to content
Closed
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
65 changes: 65 additions & 0 deletions cli/example/path-level-params.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
openapi: "3.1.0"
info:
title: "Path Level Params Test"
version: "1.0.0"
paths:
/orgs/{orgId}/items:
parameters:
- in: path
name: orgId
required: true
schema:
type: string
format: uuid
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
post:
operationId: createItem
parameters:
- in: query
name: dryRun
required: false
schema:
type: boolean
requestBody:
required: true
content:
application/json:
schema:
type: object
properties:
name:
type: string
required:
- name
responses:
"201":
description: Created
content:
application/json:
schema:
type: object
properties:
id:
type: string
format: uuid
required:
- id
29 changes: 29 additions & 0 deletions cli/example/uuid-array-param.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
openapi: "3.1.0"
info:
title: "UUID Array Test"
version: "1.0.0"
paths:
/items:
get:
operationId: getItems
parameters:
- in: query
name: ids
required: false
schema:
type: array
items:
type: string
format: uuid
responses:
"200":
description: OK
content:
application/json:
schema:
type: object
properties:
count:
type: integer
required:
- count
10 changes: 10 additions & 0 deletions cli/src/TestGenScript.elm
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ run =
singleEnum =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/single-enum.yaml")

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

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

trustmark : OpenApi.Config.Input
trustmark =
OpenApi.Config.inputFrom (OpenApi.Config.File "./example/trustmark.json")
Expand Down Expand Up @@ -135,6 +143,8 @@ run =
|> OpenApi.Config.withInput singleEnum
|> OpenApi.Config.withInput trustmark
|> OpenApi.Config.withInput trustmarkTradeCheck
|> OpenApi.Config.withInput pathLevelParams
|> OpenApi.Config.withInput uuidArrayParam
Comment thread
zupo marked this conversation as resolved.
Resolved
|> OpenApi.Config.withInput viaggiatreno
|> OpenApi.Config.withInput (bug 7889)
|> OpenApi.Config.withInput (bug 10398)
Expand Down
129 changes: 86 additions & 43 deletions src/OpenApi/Generate.elm
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,46 @@ 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)
-> List (OpenApi.Reference.ReferenceOr OpenApi.Parameter.Parameter)
mergeParams pathParams operationParams =
let
operationParamKeys : FastSet.Set String
operationParamKeys =
operationParams
|> List.filterMap
(\param ->
case OpenApi.Reference.toConcrete param of
Just concrete ->
Just (OpenApi.Parameter.in_ concrete ++ ":" ++ OpenApi.Parameter.name concrete)

Nothing ->
Nothing
)
|> FastSet.fromList

nonOverriddenPathParams : List (OpenApi.Reference.ReferenceOr OpenApi.Parameter.Parameter)
nonOverriddenPathParams =
pathParams
|> List.filter
(\param ->
case OpenApi.Reference.toConcrete param of
Just concrete ->
not (FastSet.member (OpenApi.Parameter.in_ concrete ++ ":" ++ OpenApi.Parameter.name concrete) operationParamKeys)

Nothing ->
True
)
in
nonOverriddenPathParams ++ operationParams


pathDeclarations : List OpenApi.Config.EffectType -> ServerInfo -> CliMonad (List CliMonad.Declaration)
pathDeclarations effectTypes server =
CliMonad.getApiSpec
Expand All @@ -294,7 +334,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,9 +502,12 @@ 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 =
let
allParams : List (OpenApi.Reference.ReferenceOr OpenApi.Parameter.Parameter)
allParams =
mergeParams pathLevelParams (OpenApi.Operation.parameters operation)
functionName : String
functionName =
OpenApi.Operation.operationId operation
Expand Down Expand Up @@ -1142,7 +1185,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 +1195,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 +1224,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 +1272,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 +1351,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 +1802,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 +1861,7 @@ toConfigParamAnnotation options =
, lamderaProgramTest = toAnnotation toMsgLamderaProgramTest
}
)
(operationToUrlParams options.operation)
(operationToUrlParams options.allParams)


type ServerInfo
Expand Down Expand Up @@ -1886,13 +1927,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 Expand Up @@ -2085,27 +2121,34 @@ paramToString type_ =
|> Gen.Maybe.andThen (inputToStringToFunction inputToString)
)

Common.List (Common.Basic basicType _) ->
{ inputToString =
InputToString
(\val ->
Elm.ifThen (Gen.List.call_.isEmpty val)
Gen.Maybe.make_.nothing
((case basicTypeToString basicType of
Identity ->
val

InputToString f ->
Gen.List.call_.map (Elm.functionReduced "arg" f) val
)
|> Gen.String.call_.join (Elm.string ",")
|> Gen.Maybe.make_.just
)
Common.List (Common.Basic basicType basic) ->
CliMonad.withFormat
basicType
basic.format
(\{ toParamString } -> InputToString toParamString)
(basicTypeToString basicType)
|> CliMonad.map
(\elementToString ->
{ inputToString =
InputToString
(\val ->
Elm.ifThen (Gen.List.call_.isEmpty val)
Gen.Maybe.make_.nothing
((case elementToString of
Identity ->
val

InputToString f ->
Gen.List.call_.map (Elm.functionReduced "arg" f) val
)
|> Gen.String.call_.join (Elm.string ",")
|> Gen.Maybe.make_.just
)
)
, alwaysJust = False
, isMaybe = False
}
)
, alwaysJust = False
, isMaybe = False
}
|> CliMonad.succeed

Common.List p ->
recursive p False <|
Expand Down
Loading
Loading