diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bbb50c9256..942efe67733 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). + Can be replaced with custom media types - #1462, #1548, Removed `application/octet-stream`, `text/plain`, `text/xml` [builtin support for scalar results](https://postgrest.org/en/v11.1/references/api/resource_representation.html#scalar-function-response-format) - @steve-chavez + Can be replaced with custom media types + - #1462, #1548, Removed default `application/openapi+json` media type for [db-root-spec](https://postgrest.org/en/v11.1/references/configuration.html#db-root-spec) - @steve-chavez ## [11.1.0] - 2023-06-07 diff --git a/src/PostgREST/ApiRequest.hs b/src/PostgREST/ApiRequest.hs index 6e36373aecc..7ccdac8bfde 100644 --- a/src/PostgREST/ApiRequest.hs +++ b/src/PostgREST/ApiRequest.hs @@ -15,7 +15,6 @@ module PostgREST.ApiRequest , Action(..) , Target(..) , Payload(..) - , PathInfo(..) , userApiRequest ) where @@ -127,7 +126,6 @@ data ApiRequest = ApiRequest { , iHeaders :: [(ByteString, ByteString)] -- ^ HTTP request headers , iCookies :: [(ByteString, ByteString)] -- ^ Request Cookies , iPath :: ByteString -- ^ Raw request path - , iPathInfo :: PathInfo -- ^ Cached info about the path , iMethod :: ByteString -- ^ Raw request method , iSchema :: Schema -- ^ The request schema. Can vary depending on profile headers. , iNegotiatedByProfile :: Bool -- ^ If schema was was chosen according to the profile spec https://www.w3.org/TR/dx-prof-conneg/ @@ -158,7 +156,6 @@ userApiRequest conf req reqBody = do , iHeaders = iHdrs , iCookies = iCkies , iPath = rawPathInfo req - , iPathInfo = pInfo , iMethod = method , iSchema = schema , iNegotiatedByProfile = negotiatedByProfile diff --git a/src/PostgREST/Plan.hs b/src/PostgREST/Plan.hs index c7a49e37e56..8b6fb0e3981 100644 --- a/src/PostgREST/Plan.hs +++ b/src/PostgREST/Plan.hs @@ -42,7 +42,6 @@ import PostgREST.ApiRequest (Action (..), ApiRequest (..), InvokeMethod (..), Mutation (..), - PathInfo (..), Payload (..)) import PostgREST.Config (AppConfig (..)) import PostgREST.Error (Error (..)) @@ -110,14 +109,14 @@ data InspectPlan = InspectPlan { wrappedReadPlan :: QualifiedIdentifier -> AppConfig -> SchemaCache -> ApiRequest -> Either Error WrappedReadPlan wrappedReadPlan identifier conf sCache apiRequest = do rPlan <- readPlan identifier conf sCache apiRequest - mediaType <- mapLeft ApiRequestError $ negotiateContent conf (iAction apiRequest) (iPathInfo apiRequest) (iAcceptMediaType apiRequest) + mediaType <- mapLeft ApiRequestError $ negotiateContent conf (iAction apiRequest) (iAcceptMediaType apiRequest) return $ WrappedReadPlan rPlan SQL.Read mediaType mutateReadPlan :: Mutation -> ApiRequest -> QualifiedIdentifier -> AppConfig -> SchemaCache -> Either Error MutateReadPlan mutateReadPlan mutation apiRequest identifier conf sCache = do rPlan <- readPlan identifier conf sCache apiRequest mPlan <- mutatePlan mutation identifier apiRequest sCache rPlan - mediaType <- mapLeft ApiRequestError $ negotiateContent conf (iAction apiRequest) (iPathInfo apiRequest) (iAcceptMediaType apiRequest) + mediaType <- mapLeft ApiRequestError $ negotiateContent conf (iAction apiRequest) (iAcceptMediaType apiRequest) return $ MutateReadPlan rPlan mPlan SQL.Write mediaType callReadPlan :: QualifiedIdentifier -> AppConfig -> SchemaCache -> ApiRequest -> InvokeMethod -> Either Error CallReadPlan @@ -142,7 +141,7 @@ callReadPlan identifier conf sCache apiRequest invMethod = do (InvPost, Routine.Immutable) -> SQL.Read (InvPost, Routine.Volatile) -> SQL.Write cPlan = callPlan proc apiRequest paramKeys args rPlan - mediaType <- mapLeft ApiRequestError $ negotiateContent conf (iAction apiRequest) (iPathInfo apiRequest) (iAcceptMediaType apiRequest) + mediaType <- mapLeft ApiRequestError $ negotiateContent conf (iAction apiRequest) (iAcceptMediaType apiRequest) return $ CallReadPlan rPlan cPlan txMode proc mediaType where Preferences{..} = iPreferences apiRequest @@ -150,7 +149,7 @@ callReadPlan identifier conf sCache apiRequest invMethod = do inspectPlan :: AppConfig -> ApiRequest -> Either Error InspectPlan inspectPlan conf apiRequest = do - mediaType <- mapLeft ApiRequestError $ negotiateContent conf (iAction apiRequest) (iPathInfo apiRequest) (iAcceptMediaType apiRequest) + mediaType <- mapLeft ApiRequestError $ negotiateContent conf (iAction apiRequest) (iAcceptMediaType apiRequest) return $ InspectPlan mediaType SQL.Read {-| @@ -625,28 +624,25 @@ addFilterToLogicForest :: Filter -> [LogicTree] -> [LogicTree] addFilterToLogicForest flt lf = Stmnt flt : lf -- | Do content negotiation. i.e. choose a media type based on the intersection of accepted/produced media types. -negotiateContent :: AppConfig -> Action -> PathInfo -> [MediaType] -> Either ApiRequestError MediaType -negotiateContent conf action path accepts = +negotiateContent :: AppConfig -> Action -> [MediaType] -> Either ApiRequestError MediaType +negotiateContent conf action accepts = case firstAcceptedPick of Just MTAny -> Right MTApplicationJSON -- by default(for */*) we respond with json Just mt -> Right mt Nothing -> Left . MediaTypeError $ map MediaType.toMime accepts where -- if there are multiple accepted media types, pick the first - firstAcceptedPick = listToMaybe $ L.intersect accepts $ producedMediaTypes conf action path + firstAcceptedPick = listToMaybe $ L.intersect accepts $ producedMediaTypes conf action -producedMediaTypes :: AppConfig -> Action -> PathInfo -> [MediaType] -producedMediaTypes conf action path = +producedMediaTypes :: AppConfig -> Action -> [MediaType] +producedMediaTypes conf action = case action of ActionRead _ -> defaultMediaTypes - ActionInvoke _ -> invokeMediaTypes + ActionInvoke _ -> defaultMediaTypes ActionInspect _ -> [MTOpenAPI, MTApplicationJSON, MTAny] ActionInfo -> defaultMediaTypes ActionMutate _ -> defaultMediaTypes where - invokeMediaTypes = - defaultMediaTypes - ++ [MTOpenAPI | pathIsRootSpec path] defaultMediaTypes = [MTApplicationJSON, MTSingularJSON, MTGeoJSON, MTTextCSV] ++ [MTPlan Nothing Nothing mempty | configDbPlanEnabled conf] ++ [MTAny] diff --git a/test/spec/Feature/OpenApi/RootSpec.hs b/test/spec/Feature/OpenApi/RootSpec.hs index c450d464ab4..a1ab1d78bbd 100644 --- a/test/spec/Feature/OpenApi/RootSpec.hs +++ b/test/spec/Feature/OpenApi/RootSpec.hs @@ -3,7 +3,7 @@ module Feature.OpenApi.RootSpec where import Network.HTTP.Types import Network.Wai (Application) -import Test.Hspec +import Test.Hspec hiding (pendingWith) import Test.Hspec.Wai import Test.Hspec.Wai.JSON @@ -12,7 +12,8 @@ import Protolude hiding (get) spec :: SpecWith ((), Application) spec = describe "root spec function" $ do - it "accepts application/openapi+json" $ + it "accepts application/openapi+json" $ do + pendingWith "TBD" request methodGet "/" [("Accept","application/openapi+json")] "" `shouldRespondWith` [json|{