Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow partial paths in the defaults of link properties #5560

Merged
merged 12 commits into from
Jun 13, 2023
20 changes: 19 additions & 1 deletion edb/schema/pointers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,7 @@ def _compile_expr(
make_globals_empty: bool = False,
source_context: Optional[parsing.ParserContext] = None,
detached: bool = False,
should_set_path_prefix_anchor: bool = True
) -> s_expr.CompiledExpression:
singletons: List[Union[s_types.Type, Pointer]] = []

Expand Down Expand Up @@ -1490,7 +1491,10 @@ def _compile_expr(
modaliases=context.modaliases,
schema_object_context=self.get_schema_metaclass(),
anchors={qlast.Source().name: source},
path_prefix_anchor=qlast.Source().name,
path_prefix_anchor=(
qlast.Source().name
if should_set_path_prefix_anchor
else None),
singletons=singletons,
apply_query_rewrites=(
not context.stdmode and not no_query_rewrites
Expand Down Expand Up @@ -1544,13 +1548,27 @@ def compile_expr_field(
in_ddl_context_name = None
detached = True

# If we are in a link property's default field
# do not set path prefix anchor, because link properties
# cannot have defaults that reference the object being inserted
should_set_path_prefix_anchor = True
if field.name == 'default':
# We are checking if the parent context is a pointer
# (i.e. a link or a property).
# If so, do not set the path prefix anchor.
parent_ctx = self.get_referrer_context_or_die(context)
source = parent_ctx.op.get_object(schema, context)
if isinstance(source, Pointer):
should_set_path_prefix_anchor = False

return self._compile_expr(
schema,
context,
value,
in_ddl_context_name=in_ddl_context_name,
track_schema_ref_exprs=track_schema_ref_exprs,
detached=detached,
should_set_path_prefix_anchor=should_set_path_prefix_anchor,
)
else:
return super().compile_expr_field(
Expand Down
20 changes: 20 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,26 @@ def test_schema_link_prop_on_prop_01(self):
};
"""

@tb.must_fail(errors.QueryError,
"could not resolve partial path")
def test_schema_partial_path_in_default_of_link_prop_01(self):
"""
module default {
type Person {
required name : str {
constraint exclusive;
}

multi friends : Person {
note : str {
default := .name
}
}

}
}
"""

@tb.must_fail(errors.InvalidPropertyTargetError,
"invalid property type: expected a scalar type, "
"or a scalar collection, got object type 'test::Object'",
Expand Down