diff --git a/CHANGELOG.md b/CHANGELOG.md index 329e095dfca..b8fe8c8957a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Bug fixes and improvements - server: do not serialize env vars in logs or errors for remote schemas +- server: fix regression introduced in v2.7.0-beta.1: the `kind` field in source metadata was inadvertently made a required field, breaking upgrades from v1 and new cloud projects ## v2.6.2 diff --git a/server/src-lib/Hasura/RQL/Types/Metadata.hs b/server/src-lib/Hasura/RQL/Types/Metadata.hs index c0d03476e94..5a348fd629f 100644 --- a/server/src-lib/Hasura/RQL/Types/Metadata.hs +++ b/server/src-lib/Hasura/RQL/Types/Metadata.hs @@ -103,7 +103,7 @@ where import Control.Lens hiding (set, (.=)) import Data.Aeson.Casing -import Data.Aeson.Extended (mapWithJSONPath) +import Data.Aeson.Extended (FromJSONWithContext (..), mapWithJSONPath) import Data.Aeson.Ordered qualified as AO import Data.Aeson.TH import Data.Aeson.Types @@ -465,10 +465,9 @@ deriving instance (Backend b) => Eq (SourceMetadata b) instance (Backend b) => Cacheable (SourceMetadata b) -instance (Backend b) => FromJSON (SourceMetadata b) where - parseJSON = withObject "Object" $ \o -> do +instance (Backend b) => FromJSONWithContext (BackendSourceKind b) (SourceMetadata b) where + parseJSONWithContext _smKind = withObject "Object" $ \o -> do _smName <- o .: "name" - _smKind <- o .: "kind" _smTables <- oMapFromL _tmTable <$> o .: "tables" _smFunctions <- oMapFromL _fmFunction <$> o .:? "functions" .!= [] _smConfiguration <- o .: "configuration" @@ -620,11 +619,11 @@ instance FromJSON Metadata where where parseSourceMetadata :: Value -> Parser (AB.AnyBackend SourceMetadata) parseSourceMetadata = withObject "SourceMetadata" \o -> do - backendSourceKind <- explicitParseField AB.parseBackendSourceKindFromJSON o "kind" - AB.dispatchAnyBackend'' @FromJSON @Backend + backendSourceKind <- explicitParseFieldMaybe AB.parseBackendSourceKindFromJSON o "kind" .!= AB.mkAnyBackend PostgresVanillaKind + AB.dispatchAnyBackend @Backend backendSourceKind - ( \(_kind :: BackendSourceKind b) -> - AB.mkAnyBackend @b <$> parseJSON (Object o) + ( \(kind :: BackendSourceKind b) -> + AB.mkAnyBackend @b <$> parseJSONWithContext kind (Object o) ) emptyMetadata :: Metadata diff --git a/server/tests-py/test_metadata.py b/server/tests-py/test_metadata.py index 36c8f325ba2..393bdc6bbd8 100644 --- a/server/tests-py/test_metadata.py +++ b/server/tests-py/test_metadata.py @@ -136,6 +136,39 @@ class TestMetadata: "code": "unexpected" } + """Test that missing "kind" key in metadata source defaults to "postgres". + Regression test for https://github.com/hasura/graphql-engine-mono/issues/4501""" + def test_replace_metadata_default_kind(self, hge_ctx): + st_code, resp = hge_ctx.v1metadataq({"type": "export_metadata", "args": {}}) + assert st_code == 200, resp + default_source_config = {} + default_source = list(filter(lambda source: (source["name"] == "default"), resp["sources"])) + if default_source: + default_source_config = default_source[0]["configuration"] + else: + assert False, "default source config not found" + return + st_code, resp = hge_ctx.v1metadataq({ + "type": "replace_metadata", + "version": 2, + "args": { + "metadata": { + "version": 3, + "sources": [ + { + "name": "default", + "tables": [], + "configuration": default_source_config + } + ] + } + } + }) + assert st_code == 200, resp + st_code, resp = hge_ctx.v1metadataq({"type": "export_metadata", "args": {}}) + assert st_code == 200, resp + assert resp["sources"][0]["kind"] == "postgres" + def test_dump_internal_state(self, hge_ctx): check_query_f(hge_ctx, self.dir() + '/dump_internal_state.yaml')