Skip to content

Commit

Permalink
[sc-36080] Better error on non-string enum
Browse files Browse the repository at this point in the history
  • Loading branch information
ysangkok committed Apr 9, 2024
1 parent 9ff6be9 commit 45db705
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions json-fleece-openapi3/src/Fleece/OpenApi3.hs
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ schemaTypeToParamInfo schemaMap paramName paramLocation operationKey schema =
Just text -> pure text

enumTexts <-
traverse (rejectNull <=< enumValueToText schema) enumValues
traverse (rejectNull <=< enumValueToText paramName schema) enumValues

pure
. primitiveParamInfo
Expand Down Expand Up @@ -925,7 +925,7 @@ mkOpenApiStringFormat typeName schema = do
Just enumValues ->
fmap
(CGU.enumFormat typeOptions . catMaybes)
(traverse (enumValueToText schema) enumValues)
(traverse (enumValueToText (HC.typeNameText typeName) schema) enumValues)
Nothing ->
pure $
case OA._schemaFormat schema of
Expand All @@ -940,15 +940,20 @@ mkOpenApiStringFormat typeName schema = do
CGU.LocalTimeFormat -> CGU.localTimeFormat typeOptions
_ -> CGU.textFormat typeOptions

enumValueToText :: OA.Schema -> Aeson.Value -> CGU.CodeGen (Maybe T.Text)
enumValueToText schema value =
enumValueToText :: T.Text -> OA.Schema -> Aeson.Value -> CGU.CodeGen (Maybe T.Text)
enumValueToText name schema value =
case value of
Aeson.String text -> pure (Just text)
Aeson.Null ->
case OA._schemaNullable schema of
Just True -> pure Nothing
_ -> CGU.codeGenError "null listed as enum value in a non-nullable schema"
_ -> CGU.codeGenError "Non-string value found for enum"
_ ->
CGU.codeGenError $
"Non-string value found for enum in schema/parameter titled '"
<> T.unpack name
<> "', value is "
<> show value

mkOpenApiNumberFormat :: HC.TypeName -> OA.Schema -> CGU.CodeGen CGU.CodeGenDataFormat
mkOpenApiNumberFormat typeName schema = do
Expand Down

0 comments on commit 45db705

Please sign in to comment.