Skip to content

Fix type mapping for non-public types - #4586

Merged
vonzshik merged 5 commits into
mainfrom
4582-fix-non-public-type-mapping
Aug 3, 2022
Merged

vonzshik merged 5 commits into
mainfrom
4582-fix-non-public-type-mapping

Conversation

@vonzshik

@vonzshik vonzshik commented Aug 2, 2022 •

Copy link
Copy Markdown
Contributor

Fixes #4582

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks.

For a proper cleanup in 7.0, we should decide whether we want to allow resolver to know about schemas (see comment below).

  • If we do, we can pass a QualifiedName struct (schema+name) to the resolver, and then resolvers which don't care (like the built-in one) can just match on the name.
  • If we don't, then resolver should always get a non-qualified name (we can assert on it)

Here's a test for this which you can use as a starting point. Note that it also fails on other things (e.g. reading back the correct NpgsqlParameter.DataTypeName after setting NpgsqlDbType). If it's complicated to handle those, we can defer that to 7.0, it doesn't seem completely critical (and we need to get the main fix out).

[Test, IssueLink("https://github.com/npgsql/npgsql/issues/4582")]
public async Task Type_in_non_default_schema()
{
    await using var conn = await OpenConnectionAsync();
    await using var _ = await CreateTempSchema(conn, out var schema);
    await conn.ExecuteNonQueryAsync($"DROP EXTENSION IF EXISTS citext; CREATE EXTENSION citext SCHEMA {schema}");
    try
    {
        conn.ReloadTypes();

        // await AssertType("foo", "foo", $"{schema}.citext", NpgsqlDbType.Citext, isDefaultForWriting: false, isDefaultForReading: false);
        await AssertTypeRead("foo", "foo", $"{schema}.citext", isDefault: false);
    }
    finally
    {
        await conn.ExecuteNonQueryAsync("DROP EXTENSION citext");
    }
}

Comment thread src/Npgsql/TypeMapping/ConnectorTypeMapper.cs Outdated
lock (_writeLock)
{
if ((handler = ResolveByDataTypeName(pgType.SchemaQualifiedName, throwOnError: false)) is not null)
if ((handler = ResolveByDataTypeNameCore(pgType.FullName)) is not null)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we really need this - will a resolver ever know about a specific type in a specific schema? Maybe only some special user-written one, tailored for a specific scenario or something...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably don't need this, but it's definitely a nice thing to support just in case (if we do go through with resolver cleanup in 7.0, we'll essentially going to support this anyway, just at resolver level, and not at TypeMapper).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, and there's no perf impact either, so no reason to remove it.

@vonzshik
vonzshik marked this pull request as ready for review August 3, 2022 12:06
@vonzshik
vonzshik requested a review from roji August 3, 2022 12:06

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only testing-related comments (possibly using AssertType and dropping the extension)

lock (_writeLock)
{
if ((handler = ResolveByDataTypeName(pgType.SchemaQualifiedName, throwOnError: false)) is not null)
if ((handler = ResolveByDataTypeNameCore(pgType.FullName)) is not null)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, and there's no perf impact either, so no reason to remove it.

Comment thread test/Npgsql.Tests/TypeMapperTests.cs Outdated
@vonzshik
vonzshik enabled auto-merge (squash) August 3, 2022 12:25
@vonzshik
vonzshik merged commit 438ecc8 into main Aug 3, 2022
@vonzshik
vonzshik deleted the 4582-fix-non-public-type-mapping branch August 3, 2022 12:36
vonzshik added a commit that referenced this pull request Aug 3, 2022
@vonzshik

vonzshik commented Aug 3, 2022

Copy link
Copy Markdown
Contributor Author

Backported to 6.0.6 via b3c4023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6.0.5 Regression - types in non-public schemas are resolved

2 participants