Skip to content

Commit

Permalink
fix: allow content types with additional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-bach committed Mar 8, 2024
1 parent 0013a75 commit 027a1dd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions openapi3-code-generator/src/OpenAPI/Generate/Internal/Operation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ getBodySchemaFromOperation operation = OAM.nested "requestBody" $ do

getRequestBodySchema :: OAT.RequestBodyObject -> OAM.Generator (Maybe RequestBodyDefinition, [Text])
getRequestBodySchema body = OAM.nested "content" $ do
let content = Map.lookup "application/json" $ OAT.requestBodyObjectContent body
let contentMap = OAT.requestBodyObjectContent body
content = getValueByContentTypeIgnoringCharset "application/json" contentMap
createRequestBodyDefinition encoding schema =
Just $
RequestBodyDefinition
Expand All @@ -351,7 +352,7 @@ getRequestBodySchema body = OAM.nested "content" $ do
}
case content of
Nothing ->
let formContent = Map.lookup "application/x-www-form-urlencoded" $ OAT.requestBodyObjectContent body
let formContent = getValueByContentTypeIgnoringCharset "application/x-www-form-urlencoded" contentMap
in case formContent of
Nothing -> do
OAM.logWarning "Only content type application/json and application/x-www-form-urlencoded is supported"
Expand Down Expand Up @@ -390,11 +391,19 @@ getRequestBodyObject operation =
getResponseSchema :: OAT.ResponseObject -> OAM.Generator (Maybe OAT.Schema, [Text])
getResponseSchema response = OAM.nested "content" $ do
let contentMap = OAT.responseObjectContent response
schema = Map.lookup "application/json" contentMap >>= OAT.mediaTypeObjectSchema
schema = getValueByContentTypeIgnoringCharset "application/json" contentMap >>= OAT.mediaTypeObjectSchema
when (Maybe.isNothing schema && not (Map.null contentMap)) $ OAM.logWarning "Only content type application/json is supported for response bodies."
path <- OAM.appendToPath ["application/json", "schema"]
pure (schema, path)

getValueByContentTypeIgnoringCharset :: Text -> Map.Map Text OAT.MediaTypeObject -> Maybe OAT.MediaTypeObject
getValueByContentTypeIgnoringCharset contentType contentMap =
case Map.lookup contentType contentMap of
Just content -> Just content
Nothing -> case Map.elems $ Map.filterWithKey (\key _ -> Maybe.listToMaybe (T.splitOn ";" key) == Just contentType) contentMap of
[] -> Nothing
content : _ -> Just content

-- | Resolve a possibly referenced response to a concrete value.
--
-- A warning is logged if the reference is not found.
Expand Down
2 changes: 1 addition & 1 deletion specifications/z_complex_self_made_example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ paths:
default:
description: Expected response to a valid request
content:
application/json:
application/json;charset=utf-8:
schema:
$ref: "#/components/schemas/Dog"
/pet/noparam:
Expand Down

0 comments on commit 027a1dd

Please sign in to comment.