Skip to content

Commit 2bec5cc

Browse files
committed
allow custom JSON types
1 parent a43742c commit 2bec5cc

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

openapi3-code-generator/src/OpenAPI/Generate/Internal/Operation.hs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ getBodySchemaFromOperation operation = OAM.nested "requestBody" $ do
342342
getRequestBodySchema :: OAT.RequestBodyObject -> OAM.Generator (Maybe RequestBodyDefinition, [Text])
343343
getRequestBodySchema body = OAM.nested "content" $ do
344344
let contentMap = OAT.requestBodyObjectContent body
345-
content = getValueByContentTypeIgnoringCharset "application/json" contentMap
345+
content = getJsonMediaTypeObject contentMap
346346
createRequestBodyDefinition encoding schema =
347347
Just $
348348
RequestBodyDefinition
@@ -391,7 +391,7 @@ getRequestBodyObject operation =
391391
getResponseSchema :: OAT.ResponseObject -> OAM.Generator (Maybe OAT.Schema, [Text])
392392
getResponseSchema response = OAM.nested "content" $ do
393393
let contentMap = OAT.responseObjectContent response
394-
schema = getValueByContentTypeIgnoringCharset "application/json" contentMap >>= OAT.mediaTypeObjectSchema
394+
schema = getJsonMediaTypeObject contentMap >>= OAT.mediaTypeObjectSchema
395395
when (Maybe.isNothing schema && not (Map.null contentMap)) $ OAM.logWarning "Only content type application/json is supported for response bodies."
396396
path <- OAM.appendToPath ["application/json", "schema"]
397397
pure (schema, path)
@@ -400,10 +400,26 @@ getValueByContentTypeIgnoringCharset :: Text -> Map.Map Text OAT.MediaTypeObject
400400
getValueByContentTypeIgnoringCharset contentType contentMap =
401401
case Map.lookup contentType contentMap of
402402
Just content -> Just content
403-
Nothing -> case Map.elems $ Map.filterWithKey (\key _ -> Maybe.listToMaybe (T.splitOn ";" key) == Just contentType) contentMap of
403+
Nothing -> case Map.elems $ Map.filterWithKey (\key _ -> getMediaTypeWithoutCharset key == Just contentType) contentMap of
404404
[] -> Nothing
405405
content : _ -> Just content
406406

407+
getMediaTypeWithoutCharset :: Text -> Maybe Text
408+
getMediaTypeWithoutCharset = Maybe.listToMaybe . T.splitOn ";"
409+
410+
getJsonMediaTypeObject :: Map.Map Text OAT.MediaTypeObject -> Maybe OAT.MediaTypeObject
411+
getJsonMediaTypeObject contentMap =
412+
case getValueByContentTypeIgnoringCharset "application/json" contentMap of
413+
Just content -> Just content
414+
Nothing -> case Map.elems $ Map.filterWithKey (\key _ -> maybe False isCustomJsonMediaType $ Maybe.listToMaybe (T.splitOn ";" key)) contentMap of
415+
[] -> Nothing
416+
content : _ -> Just content
417+
418+
isCustomJsonMediaType :: Text -> Bool
419+
isCustomJsonMediaType mediaType = case T.splitOn "+" mediaType of
420+
[_, "json"] -> True
421+
_ -> False
422+
407423
-- | Resolve a possibly referenced response to a concrete value.
408424
--
409425
-- A warning is logged if the reference is not found.

specifications/z_complex_self_made_example.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ paths:
6060
'200':
6161
description: successful operation
6262
content:
63-
application/json:
63+
application/problem+json:
6464
schema:
6565
$ref: "#/components/schemas/Dog"
6666
/pet/singleparam:

0 commit comments

Comments
 (0)