Skip to content

Commit

Permalink
fix: remove rejected mediatype application/vnd.pgrst.object+json from…
Browse files Browse the repository at this point in the history
… response
  • Loading branch information
taimoorzaeem authored and steve-chavez committed Apr 13, 2024
1 parent fbc4d56 commit 973201a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- #3340, Log when the LISTEN channel gets a notification - @steve-chavez
- #3345, Fix in-database configuration values not loading for `pgrst.server_trace_header` and `pgrst.server_cors_allowed_origins` - @laurenceisla
- #3361, Clarify PGRST204(column not found) error message - @steve-chavez
- #3373, Remove rejected mediatype `application/vnd.pgrst.object+json` from response - @taimoorzaeem

### Deprecated

Expand Down
1 change: 0 additions & 1 deletion src/PostgREST/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ instance PgrstError ApiRequestError where
status PGRSTParseError = HTTP.status500
status MaxAffectedViolationError{} = HTTP.status400

headers SingularityError{} = [MediaType.toContentType $ MTVndSingularJSON False]
headers _ = mempty

toJsonPgrstError :: ErrorCode -> Text -> Maybe JSON.Value -> Maybe JSON.Value -> JSON.Value
Expand Down
2 changes: 1 addition & 1 deletion test/spec/Feature/Query/NullsStripSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 6 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [matchContentTypeSingular]
, matchHeaders = [matchContentTypeJson]
}

context "strip nulls from response even if explicitly selected" $ do
Expand Down
32 changes: 16 additions & 16 deletions test/spec/Feature/Query/SingularSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 4 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular ]
, matchHeaders = [ matchContentTypeJson ]
}

-- the rows should not be updated, either
Expand All @@ -87,7 +87,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 4 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular ]
, matchHeaders = [ matchContentTypeJson ]
}

-- the rows should not be updated, either
Expand All @@ -101,7 +101,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 0 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [matchContentTypeSingular]
, matchHeaders = [matchContentTypeJson]
}

it "raises an error for zero rows with return=rep" $
Expand All @@ -110,7 +110,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 0 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [matchContentTypeSingular]
, matchHeaders = [matchContentTypeJson]
}

context "when creating rows" $ do
Expand Down Expand Up @@ -143,7 +143,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 2 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular ]
, matchHeaders = [ matchContentTypeJson ]
}

-- the rows should not exist, either
Expand All @@ -158,7 +158,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 2 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular ]
, matchHeaders = [ matchContentTypeJson ]
}

-- the rows should not exist, either
Expand All @@ -173,7 +173,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 2 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular ]
, matchHeaders = [ matchContentTypeJson ]
}

-- the rows should not exist, either
Expand All @@ -188,7 +188,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 0 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [matchContentTypeSingular]
, matchHeaders = [matchContentTypeJson]
}

it "raises an error when creating zero entities with return=rep" $
Expand All @@ -198,7 +198,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 0 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [matchContentTypeSingular]
, matchHeaders = [matchContentTypeJson]
}

context "when deleting rows" $ do
Expand All @@ -221,7 +221,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 5 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular ]
, matchHeaders = [ matchContentTypeJson ]
}

-- the rows should still exist
Expand All @@ -238,7 +238,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 5 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular ]
, matchHeaders = [ matchContentTypeJson ]
}

-- the rows should still exist
Expand All @@ -254,7 +254,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 0 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [matchContentTypeSingular]
, matchHeaders = [matchContentTypeJson]
}

it "raises an error when deleting zero entities with return=rep" $
Expand All @@ -263,7 +263,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 0 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [matchContentTypeSingular]
, matchHeaders = [matchContentTypeJson]
}

context "when calling a stored proc" $ do
Expand All @@ -273,7 +273,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 0 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [matchContentTypeSingular]
, matchHeaders = [matchContentTypeJson]
}

-- this one may be controversial, should vnd.pgrst.object include
Expand All @@ -296,7 +296,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 5 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [matchContentTypeSingular]
, matchHeaders = [matchContentTypeJson]
}

it "fails for multiple rows with rolled back changes" $ do
Expand All @@ -311,7 +311,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 2 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular]
, matchHeaders = [ matchContentTypeJson ]
}

-- should rollback function
Expand Down
21 changes: 17 additions & 4 deletions test/spec/SpecHelper.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE FlexibleContexts #-}
module SpecHelper where

import Control.Lens ((^?))
Expand Down Expand Up @@ -36,17 +37,29 @@ import PostgREST.SchemaCache.Identifiers (QualifiedIdentifier (..))
import Protolude hiding (get, toS)
import Protolude.Conv (toS)

filterAndMatchCT :: BS.ByteString -> MatchHeader
filterAndMatchCT val = MatchHeader $ \headers _ ->
case filter (\(n,_) -> n == hContentType) headers of
[(_,v)] -> if v == val
then Nothing
else Just $ "missing value:" <> toS val <> "\n"
_ -> Just "unexpected header: zero or multiple headers present\n"

matchContentTypeJson :: MatchHeader
matchContentTypeJson = "Content-Type" <:> "application/json; charset=utf-8"
matchContentTypeJson =
filterAndMatchCT "application/json; charset=utf-8"

matchContentTypeSingular :: MatchHeader
matchContentTypeSingular = "Content-Type" <:> "application/vnd.pgrst.object+json; charset=utf-8"
matchContentTypeSingular =
filterAndMatchCT "application/vnd.pgrst.object+json; charset=utf-8"

matchCTArrayStrip :: MatchHeader
matchCTArrayStrip = "Content-Type" <:> "application/vnd.pgrst.array+json;nulls=stripped; charset=utf-8"
matchCTArrayStrip =
filterAndMatchCT "application/vnd.pgrst.array+json;nulls=stripped; charset=utf-8"

matchCTSingularStrip :: MatchHeader
matchCTSingularStrip = "Content-Type" <:> "application/vnd.pgrst.object+json;nulls=stripped; charset=utf-8"
matchCTSingularStrip =
filterAndMatchCT "application/vnd.pgrst.object+json;nulls=stripped; charset=utf-8"

matchHeaderValuePresent :: HeaderName -> BS.ByteString -> MatchHeader
matchHeaderValuePresent name val = MatchHeader $ \headers _ ->
Expand Down

0 comments on commit 973201a

Please sign in to comment.