@@ -29,6 +29,8 @@ module Yoga.HTTP.API.Route.OpenAPI
2929 , renderVariantResponseSchema
3030 , class RenderVariantResponseSchemaRL
3131 , renderVariantResponseSchemaRL
32+ , class RenderResponseContent
33+ , renderResponseContent
3234 , class RenderJSONSchema
3335 , renderJSONSchema
3436 , class RenderRecordSchemaRL
@@ -103,7 +105,9 @@ import Yoga.HTTP.API.Route.Encoding (JSON, FormData, MultipartFormData, PlainTex
103105import Yoga.HTTP.API.Route.HeaderValue (class HeaderValueType, headerValueType)
104106import Yoga.HTTP.API.Route.OpenAPIMetadata (Description, Example, Format, Minimum, Maximum, Pattern, MinLength, MaxLength, Title, Nullable, Default, Deprecated, Enum, Schema, Callback, Examples, class HasDescription, description, class HasExample, example, class HasFormat, format, class HasDeprecated, deprecated, class HasMinimum, minimum, class HasMaximum, maximum, class HasPattern, pattern, class HasMinLength, minLength, class HasMaxLength, maxLength, class HasTitle, title, class HasNullable, nullable, class HasDefault, default, class HasEnum, enum, class HasExamples, examples)
105107import Yoga.HTTP.API.Route.RenderMethod (class RenderMethod, renderMethod)
106−import Yoga.HTTP.API.Route.Response (class ToResponse)
108+import Prim.Boolean (True, False)
109+import Data.Unit (Unit)
110+import Yoga.HTTP.API.Route.Response (class ToResponse, class IsUnitBody)
107111import Yoga.HTTP.API.Route.StatusCode (class StatusCodeMap, statusCodeFor, statusCodeToString)
108112import Yoga.JSON (class WriteForeign, writeImpl, unsafeStringify)
109113import Yoga.JSON as Yoga.JSON
@@ -1412,10 +1416,9 @@ instance renderVariantResponseSchemaRLCons ::
14121416 ( IsSymbol label
14131417 , StatusCodeMap label
14141418 , ToResponse recordType headers body
1419+ , IsUnitBody body isUnit
1420+ , RenderResponseContent isUnit headers body
14151421 , RenderResponseHeadersSchema headers
1416− , RenderJSONSchema body
1417− , GetContentType body
1418− -- , HasLinks recordType
14191422 , RenderVariantResponseSchemaRL tail
14201423 ) =>
14211424 RenderVariantResponseSchemaRL (Cons label recordType tail) where
@@ -1423,30 +1426,36 @@ instance renderVariantResponseSchemaRLCons ::
14231426 let
14241427 statusCode = statusCodeFor (Proxy :: Proxy label)
14251428 statusCodeStr = statusCodeToString statusCode
1426− headersObj = renderResponseHeadersSchema (Proxy :: Proxy headers)
1427− bodySchema = renderJSONSchema (Proxy :: Proxy body)
1428− contentType = getContentType (Proxy :: Proxy body)
1429− contentObj = FObject.singleton contentType (unsafeCoerce { schema: bodySchema })
1430− -- linksArray = links (Proxy :: Proxy recordType)
1431− -- linksObj =
1432− -- if Array.null linksArray then FObject.empty
1433− -- else FObject.fromFoldable $ linksArray <#> \link ->
1434− -- Tuple link.name $ unsafeCoerce $ FObject.fromFoldable
1435− -- [ Tuple "operationId" (unsafeCoerce link.operationId)
1436− -- , Tuple "parameters" link.parameters
1437− -- ]
1438− responseObjBase =
1439− { description: "Successful response"
1440− , headers: headersObj
1441− , content: unsafeCoerce contentObj
1442− }
1443− responseObj = unsafeCoerce responseObjBase
1444− -- if FObject.isEmpty linksObj then unsafeCoerce responseObjBase
1445− -- else unsafeCoerce $ FObject.insert "links" (unsafeCoerce linksObj) (unsafeCoerce responseObjBase :: FObject.Object Foreign)
1429+ responseObj = renderResponseContent (Proxy :: Proxy isUnit) (Proxy :: Proxy headers) (Proxy :: Proxy body)
14461430 rest = renderVariantResponseSchemaRL (Proxy :: Proxy tail)
14471431 in
14481432 FObject.insert statusCodeStr responseObj rest
14491433
1434+class RenderResponseContent (isUnit :: Boolean) (headers :: Row Type) (body :: Type) where
1435+ renderResponseContent :: Proxy isUnit -> Proxy headers -> Proxy body -> ResponseObject
1436+
1437+-- Unit body: no content field
1438+instance RenderResponseHeadersSchema headers =>
1439+ RenderResponseContent True headers Unit where
1440+ renderResponseContent _ _ _ =
1441+ { description: "No content"
1442+ , headers: renderResponseHeadersSchema (Proxy :: Proxy headers)
1443+ , content: unsafeCoerce (FObject.empty :: FObject.Object Foreign)
1444+ }
1445+
1446+-- Non-Unit body: render content schema
1447+else instance (RenderResponseHeadersSchema headers, RenderJSONSchema body, GetContentType body) =>
1448+ RenderResponseContent False headers body where
1449+ renderResponseContent _ _ _ = do
1450+ let headersObj = renderResponseHeadersSchema (Proxy :: Proxy headers)
1451+ let bodySchema = renderJSONSchema (Proxy :: Proxy body)
1452+ let contentType = getContentType (Proxy :: Proxy body)
1453+ let contentObj = FObject.singleton contentType (unsafeCoerce { schema: bodySchema })
1454+ { description: "Successful response"
1455+ , headers: headersObj
1456+ , content: unsafeCoerce contentObj
1457+ }
1458+
14501459-- --------------------------------------------------------------------------------
14511460-- -- RenderCallbacks: Render full callback objects for OpenAPI
14521461-- --------------------------------------------------------------------------------