Skip to content

allow custom JSON types #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading