diff --git a/CHANGELOG.md b/CHANGELOG.md index 524f870cf..69ea3ba9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## Unreleased + +### Fixed + + - #2824, Fix range request with 0 rows and 0 offset return status 416 - @strengthless + ## [11.2.1] - 2023-10-03 ### Fixed diff --git a/src/PostgREST/RangeQuery.hs b/src/PostgREST/RangeQuery.hs index c042a3735..215dba62b 100644 --- a/src/PostgREST/RangeQuery.hs +++ b/src/PostgREST/RangeQuery.hs @@ -104,9 +104,9 @@ rangeStatusHeader topLevelRange queryTotal tableTotal = rangeStatus :: Integer -> Integer -> Maybe Integer -> Status rangeStatus _ _ Nothing = status200 rangeStatus lower upper (Just total) - | lower >= total && lower /= upper = status416 -- 416 Range Not Satisfiable - | (1 + upper - lower) < total = status206 -- 206 Partial Content - | otherwise = status200 -- 200 OK + | lower >= total && lower /= upper && lower /= 0 = status416 -- 416 Range Not Satisfiable + | (1 + upper - lower) < total = status206 -- 206 Partial Content + | otherwise = status200 -- 200 OK contentRangeH :: (Integral a, Show a) => a -> a -> Maybe a -> Header contentRangeH lower upper total = diff --git a/test/spec/Feature/Query/RangeSpec.hs b/test/spec/Feature/Query/RangeSpec.hs index 8a983fbcb..0aaee3a2c 100644 --- a/test/spec/Feature/Query/RangeSpec.hs +++ b/test/spec/Feature/Query/RangeSpec.hs @@ -413,6 +413,15 @@ spec = do matchHeader "Content-Range" "10-14/*" simpleStatus r `shouldBe` ok200 + it "does not throw error when offset is 0 and and total is 0" $ + request methodGet "/rpc/getitemrange?min=0&max=0" + (rangeHdrsWithCount $ ByteRangeFromTo 0 1) mempty + `shouldRespondWith` + [json|[]|] + { matchStatus = 200 + , matchHeaders = ["Content-Range" <:> "*/0"] + } + context "of invalid range" $ do it "fails with 416 for offside range" $ request methodGet "/items"