Skip to content

Commit

Permalink
allow custom JSON types
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-bach committed May 29, 2024
1 parent a43742c commit 2bec5cc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
22 changes: 19 additions & 3 deletions openapi3-code-generator/src/OpenAPI/Generate/Internal/Operation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ getBodySchemaFromOperation operation = OAM.nested "requestBody" $ do
getRequestBodySchema :: OAT.RequestBodyObject -> OAM.Generator (Maybe RequestBodyDefinition, [Text])
getRequestBodySchema body = OAM.nested "content" $ do
let contentMap = OAT.requestBodyObjectContent body
content = getValueByContentTypeIgnoringCharset "application/json" contentMap
content = getJsonMediaTypeObject contentMap
createRequestBodyDefinition encoding schema =
Just $
RequestBodyDefinition
Expand Down Expand Up @@ -391,7 +391,7 @@ getRequestBodyObject operation =
getResponseSchema :: OAT.ResponseObject -> OAM.Generator (Maybe OAT.Schema, [Text])
getResponseSchema response = OAM.nested "content" $ do
let contentMap = OAT.responseObjectContent response
schema = getValueByContentTypeIgnoringCharset "application/json" contentMap >>= OAT.mediaTypeObjectSchema
schema = getJsonMediaTypeObject 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)
Expand All @@ -400,10 +400,26 @@ getValueByContentTypeIgnoringCharset :: Text -> Map.Map Text 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 -> case Map.elems $ Map.filterWithKey (\key _ -> getMediaTypeWithoutCharset key == Just contentType) contentMap of
[] -> Nothing
content : _ -> Just content

getMediaTypeWithoutCharset :: Text -> Maybe Text
getMediaTypeWithoutCharset = Maybe.listToMaybe . T.splitOn ";"

getJsonMediaTypeObject :: Map.Map Text OAT.MediaTypeObject -> Maybe OAT.MediaTypeObject
getJsonMediaTypeObject contentMap =
case getValueByContentTypeIgnoringCharset "application/json" contentMap of
Just content -> Just content
Nothing -> case Map.elems $ Map.filterWithKey (\key _ -> maybe False isCustomJsonMediaType $ Maybe.listToMaybe (T.splitOn ";" key)) contentMap of
[] -> Nothing
content : _ -> Just content

isCustomJsonMediaType :: Text -> Bool
isCustomJsonMediaType mediaType = case T.splitOn "+" mediaType of
[_, "json"] -> True
_ -> False

-- | 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 @@ -60,7 +60,7 @@ paths:
'200':
description: successful operation
content:
application/json:
application/problem+json:
schema:
$ref: "#/components/schemas/Dog"
/pet/singleparam:
Expand Down

0 comments on commit 2bec5cc

Please sign in to comment.