From b19b21101db572786b6b35b51203df27b024c7f6 Mon Sep 17 00:00:00 2001 From: Neyts Zupan Date: Fri, 6 Mar 2026 17:32:46 +0000 Subject: [PATCH 1/2] Fix UUID array query params to use format-aware conversion The Common.List (Common.Basic ...) branch in paramToString was ignoring the format field, so UUID arrays used plain string identity instead of Uuid.toString. Now uses CliMonad.withFormat like the non-list Basic case. Add test, example spec, and TestGenScript entry for uuid-array-param. --- cli/example/uuid-array-param.yaml | 29 +++++ cli/src/TestGenScript.elm | 5 + src/OpenApi/Generate.elm | 47 ++++---- tests/Test/OpenApi/Generate.elm | 192 +++++++++++++++++++++++++++++- 4 files changed, 249 insertions(+), 24 deletions(-) create mode 100644 cli/example/uuid-array-param.yaml diff --git a/cli/example/uuid-array-param.yaml b/cli/example/uuid-array-param.yaml new file mode 100644 index 00000000..25a0276f --- /dev/null +++ b/cli/example/uuid-array-param.yaml @@ -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 diff --git a/cli/src/TestGenScript.elm b/cli/src/TestGenScript.elm index a7a30280..4944fad4 100644 --- a/cli/src/TestGenScript.elm +++ b/cli/src/TestGenScript.elm @@ -76,6 +76,10 @@ 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") + trustmark : OpenApi.Config.Input trustmark = OpenApi.Config.inputFrom (OpenApi.Config.File "./example/trustmark.json") @@ -135,6 +139,7 @@ run = |> OpenApi.Config.withInput singleEnum |> OpenApi.Config.withInput trustmark |> OpenApi.Config.withInput trustmarkTradeCheck + |> OpenApi.Config.withInput uuidArrayParam |> OpenApi.Config.withInput viaggiatreno |> OpenApi.Config.withInput (bug 7889) |> OpenApi.Config.withInput (bug 10398) diff --git a/src/OpenApi/Generate.elm b/src/OpenApi/Generate.elm index 3b7faf9c..b7853ff7 100644 --- a/src/OpenApi/Generate.elm +++ b/src/OpenApi/Generate.elm @@ -2085,27 +2085,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 <| diff --git a/tests/Test/OpenApi/Generate.elm b/tests/Test/OpenApi/Generate.elm index cbd9f193..76a4235d 100644 --- a/tests/Test/OpenApi/Generate.elm +++ b/tests/Test/OpenApi/Generate.elm @@ -1,4 +1,4 @@ -module Test.OpenApi.Generate exposing (fuzzInputName, fuzzTitle, issue48, pr267) +module Test.OpenApi.Generate exposing (fuzzInputName, fuzzTitle, issue48, pr267, uuidArrayParam) import Ansi.Color import CliMonad @@ -478,6 +478,177 @@ pr267 = ) +uuidArrayParam : Test +uuidArrayParam = + Test.test "UUID array query params use Uuid.toString" <| + \() -> + let + oasString : String + oasString = + String.Multiline.here """ + 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 + """ + in + case + oasString + |> Yaml.Decode.fromString yamlToJsonValueDecoder + |> Result.mapError Debug.toString + |> Result.andThen + (\json -> + json + |> Json.Decode.decodeValue OpenApi.decode + |> Result.mapError Debug.toString + ) + of + Err e -> + Expect.fail e + + Ok oas -> + let + genFiles : + Result + CliMonad.Message + { modules : + List + { moduleName : List String + , declarations : FastDict.Dict String { group : String, declaration : Elm.Declaration } + } + , warnings : List CliMonad.Message + , requiredPackages : FastSet.Set String + } + genFiles = + OpenApi.Generate.files + { namespace = [ "Output" ] + , generateTodos = False + , effectTypes = [ OpenApi.Config.ElmHttpCmd ] + , server = OpenApi.Config.Default + , formats = OpenApi.Config.defaultFormats + , warnOnMissingEnums = True + , keepGoing = False + } + oas + in + case genFiles of + Err e -> + Expect.fail ("Error in generation: " ++ Debug.toString e) + + Ok { modules } -> + case modules of + [ _, apiFile ] -> + let + apiFileString : String + apiFileString = + String.Multiline.here """ + module Output.Api exposing ( getItems ) + + {-| + @docs getItems + -} + + + import Dict + import Http + import Json.Decode + import OpenApi.Common + import Url.Builder + import Uuid + + + {- ## Operations -} + + + getItems : + { toMsg : Result (OpenApi.Common.Error e String) { count : Int } -> msg + , params : { ids : Maybe (List Uuid.Uuid) } + } + -> Cmd msg + getItems config = + Http.request + { url = + Url.Builder.absolute + [ "items" ] + (List.filterMap + Basics.identity + [ Maybe.map + (Url.Builder.string "ids") + (Maybe.andThen + (\\andThenUnpack -> + if List.isEmpty andThenUnpack then + Nothing + + else + Just + (String.join + "," + (List.map + OpenApi.Common.toParamStringStringUuid + andThenUnpack + ) + ) + ) + config.params.ids + ) + ] + ) + , method = "GET" + , headers = [] + , expect = + OpenApi.Common.expectJsonCustom + (Dict.fromList []) + (Json.Decode.succeed + (\\count -> { count = count } + ) |> OpenApi.Common.jsonDecodeAndMap + (Json.Decode.field "count" Json.Decode.int) + ) + config.toMsg + , body = Http.emptyBody + , timeout = Nothing + , tracker = Nothing + } + """ + in + expectEqualMultiline apiFileString (fileToString apiFile) + + [] -> + Expect.fail "Expected to generate 2 files but found none" + + _ -> + Expect.fail + ("Expected to generate 2 files but found " + ++ (List.length modules |> String.fromInt) + ++ ": " + ++ moduleNames modules + ) + + yamlToJsonValueDecoder : Yaml.Decode.Decoder Json.Encode.Value yamlToJsonValueDecoder = Yaml.Decode.oneOf @@ -511,7 +682,20 @@ fileToString file = expectEqualMultiline : String -> String -> Expect.Expectation expectEqualMultiline exp actual = - if exp == actual then + let + trimTrailingWhitespace : String -> String + trimTrailingWhitespace = + String.lines >> List.map String.trimRight >> String.join "\n" + + normalizedExp : String + normalizedExp = + trimTrailingWhitespace exp + + normalizedActual : String + normalizedActual = + trimTrailingWhitespace actual + in + if normalizedExp == normalizedActual then Expect.pass else @@ -527,8 +711,8 @@ expectEqualMultiline exp actual = (Diff.defaultOptions |> Diff.ignoreLeadingWhitespace ) - exp - actual + normalizedExp + normalizedActual |> Diff.ToString.diffToString { context = 4, color = True } ) ) From f824aa7f7c4ef7fe7370c4ec2f25258a6299bf3a Mon Sep 17 00:00:00 2001 From: Neyts Zupan Date: Fri, 6 Mar 2026 22:23:47 +0000 Subject: [PATCH 2/2] Support path-level parameters in code generation Merge path item parameters with operation parameters per the OpenAPI spec. Previously, parameters defined at the path level (shared across all operations) were silently ignored, causing path params like {orgId} to appear as literal strings in generated URLs. --- cli/example/path-level-params.yaml | 65 ++++++++++++ cli/src/TestGenScript.elm | 5 + src/OpenApi/Generate.elm | 82 +++++++++++---- tests/Test/OpenApi/Generate.elm | 164 ++++++++++++++++++++++++++++- 4 files changed, 292 insertions(+), 24 deletions(-) create mode 100644 cli/example/path-level-params.yaml diff --git a/cli/example/path-level-params.yaml b/cli/example/path-level-params.yaml new file mode 100644 index 00000000..1e6306ec --- /dev/null +++ b/cli/example/path-level-params.yaml @@ -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 diff --git a/cli/src/TestGenScript.elm b/cli/src/TestGenScript.elm index 4944fad4..133600f8 100644 --- a/cli/src/TestGenScript.elm +++ b/cli/src/TestGenScript.elm @@ -76,6 +76,10 @@ 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") @@ -139,6 +143,7 @@ run = |> OpenApi.Config.withInput singleEnum |> OpenApi.Config.withInput trustmark |> OpenApi.Config.withInput trustmarkTradeCheck + |> OpenApi.Config.withInput pathLevelParams |> OpenApi.Config.withInput uuidArrayParam |> OpenApi.Config.withInput viaggiatreno |> OpenApi.Config.withInput (bug 7889) diff --git a/src/OpenApi/Generate.elm b/src/OpenApi/Generate.elm index b7853ff7..0fc6675d 100644 --- a/src/OpenApi/Generate.elm +++ b/src/OpenApi/Generate.elm @@ -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 @@ -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) @@ -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 @@ -1142,7 +1185,7 @@ toRequestFunctions server effectTypes method pathUrl operation = |> CliMonad.andThen (\params -> toConfigParamAnnotation - { operation = operation + { allParams = allParams , successAnnotation = successAnnotation , errorBodyAnnotation = bodyTypeAnnotation , errorTypeAnnotation = errorTypeAnnotation @@ -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) @@ -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 @@ -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 = @@ -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 @@ -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 @@ -1820,7 +1861,7 @@ toConfigParamAnnotation options = , lamderaProgramTest = toAnnotation toMsgLamderaProgramTest } ) - (operationToUrlParams options.operation) + (operationToUrlParams options.allParams) type ServerInfo @@ -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 [] diff --git a/tests/Test/OpenApi/Generate.elm b/tests/Test/OpenApi/Generate.elm index 76a4235d..3c1c1918 100644 --- a/tests/Test/OpenApi/Generate.elm +++ b/tests/Test/OpenApi/Generate.elm @@ -1,4 +1,4 @@ -module Test.OpenApi.Generate exposing (fuzzInputName, fuzzTitle, issue48, pr267, uuidArrayParam) +module Test.OpenApi.Generate exposing (fuzzInputName, fuzzTitle, issue48, pathLevelParams, pr267, uuidArrayParam) import Ansi.Color import CliMonad @@ -649,6 +649,168 @@ uuidArrayParam = ) +pathLevelParams : Test +pathLevelParams = + Test.test "Path-level parameters are merged into operations" <| + \() -> + let + oasString : String + oasString = + String.Multiline.here """ + 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 + """ + in + case + oasString + |> Yaml.Decode.fromString yamlToJsonValueDecoder + |> Result.mapError Debug.toString + |> Result.andThen + (\json -> + json + |> Json.Decode.decodeValue OpenApi.decode + |> Result.mapError Debug.toString + ) + of + Err e -> + Expect.fail e + + Ok oas -> + let + genFiles : + Result + CliMonad.Message + { modules : + List + { moduleName : List String + , declarations : FastDict.Dict String { group : String, declaration : Elm.Declaration } + } + , warnings : List CliMonad.Message + , requiredPackages : FastSet.Set String + } + genFiles = + OpenApi.Generate.files + { namespace = [ "Output" ] + , generateTodos = False + , effectTypes = [ OpenApi.Config.ElmHttpCmd ] + , server = OpenApi.Config.Default + , formats = OpenApi.Config.defaultFormats + , warnOnMissingEnums = True + , keepGoing = False + } + oas + in + case genFiles of + Err e -> + Expect.fail ("Error in generation: " ++ Debug.toString e) + + Ok { modules } -> + case modules of + [ _, apiFile ] -> + let + apiFileString : String + apiFileString = + String.Multiline.here """ + module Output.Api exposing ( getItems ) + + {-| + @docs getItems + -} + + + import Dict + import Http + import Json.Decode + import OpenApi.Common + import Url.Builder + import Uuid + + + {- ## Operations -} + + + getItems : + { toMsg : Result (OpenApi.Common.Error e String) { count : Int } -> msg + , params : { orgId : Uuid.Uuid, status : Maybe String } + } + -> Cmd msg + getItems config = + Http.request + { url = + Url.Builder.absolute + [ "orgs" + , OpenApi.Common.toParamStringStringUuid config.params.orgId + , "items" + ] + (List.filterMap + Basics.identity + [ Maybe.map + (Url.Builder.string "status") + config.params.status + ] + ) + , method = "GET" + , headers = [] + , expect = + OpenApi.Common.expectJsonCustom + (Dict.fromList []) + (Json.Decode.succeed + (\\count -> { count = count } + ) |> OpenApi.Common.jsonDecodeAndMap + (Json.Decode.field "count" Json.Decode.int) + ) + config.toMsg + , body = Http.emptyBody + , timeout = Nothing + , tracker = Nothing + } + """ + in + expectEqualMultiline apiFileString (fileToString apiFile) + + [] -> + Expect.fail "Expected to generate 2 files but found none" + + _ -> + Expect.fail + ("Expected to generate 2 files but found " + ++ (List.length modules |> String.fromInt) + ++ ": " + ++ moduleNames modules + ) + + yamlToJsonValueDecoder : Yaml.Decode.Decoder Json.Encode.Value yamlToJsonValueDecoder = Yaml.Decode.oneOf