Skip to content

Commit

Permalink
Change parsing to avoid Yaml parser if likely JSON
Browse files Browse the repository at this point in the history
Now checks if the input filepath ends with .json giving the user
a way to avoid the Yaml parser as it can do very strange things to
the JSON values as noted in issue wolfadex#53.
  • Loading branch information
lawik committed Oct 31, 2023
1 parent a66970b commit 28fa12c
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/Cli.elm
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ run =
BackendTask.File.DecodingError _ ->
"Uh oh! Decoding failure!"
)
|> BackendTask.andThen decodeOpenApiSpecOrFail
|> BackendTask.andThen (decodeOpenApiSpecOrFail entryFilePath)
|> BackendTask.andThen
(generateFileFromOpenApiSpec
{ outputDirectory = outputDirectory
Expand All @@ -73,18 +73,27 @@ run =
)


decodeOpenApiSpecOrFail : String -> BackendTask.BackendTask FatalError.FatalError OpenApi.OpenApi
decodeOpenApiSpecOrFail input =
decodeOpenApiSpecOrFail : String -> String -> BackendTask.BackendTask FatalError.FatalError OpenApi.OpenApi
decodeOpenApiSpecOrFail filePath input =
let
-- TODO: Better handling of errors: https://github.com/wolfadex/elm-api-sdk-generator/issues/40
isJson =
String.endsWith ".json" filePath

decoded : Result Json.Decode.Error OpenApi.OpenApi
decoded =
case Yaml.Decode.fromString yamlToJsonDecoder input of
Err _ ->
Json.Decode.decodeString OpenApi.decode input

Ok jsonFromYaml ->
Json.Decode.decodeValue OpenApi.decode jsonFromYaml
-- Short-circuit the error-prone yaml parsing of JSON structures if we
-- are reasonably confident that it is a JSON file
if isJson then
Json.Decode.decodeString OpenApi.decode input

else
case Yaml.Decode.fromString yamlToJsonDecoder input of
Err _ ->
Json.Decode.decodeString OpenApi.decode input

Ok jsonFromYaml ->
Json.Decode.decodeValue OpenApi.decode jsonFromYaml
in
decoded
|> Result.mapError
Expand Down

0 comments on commit 28fa12c

Please sign in to comment.