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

TypeNotDefined error on a minimal schema w/ built-in types #496

Open
3 tasks done
ericox opened this issue Dec 17, 2024 · 4 comments
Open
3 tasks done

TypeNotDefined error on a minimal schema w/ built-in types #496

ericox opened this issue Dec 17, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@ericox
Copy link

ericox commented Dec 17, 2024

Before opening, please confirm:

Bug Category

Schemas and Validation

Describe the bug

A valid schema fails with a TypeNotDefined error when building a raw schema fragment.

Expected behavior

The schema fragment should be constructed w/o panicking.

Reproduction steps

  1. create a Cedar schema file on local disk with one entity
entity User {
   id: String,
    name: String,
 }
  1. run the code snipped below in a main().

Code Snippet

Given a schema with a single entity definition

entity User {
    id: String,
    name: String,
};

Loading a schema from a file,

  let buf = build_buffer();
  let mut u = Unstructured::new(&buf);
  let fragment = json_schema::Fragment::<RawName>::from_cedarschema_file(
        File::open(Path::new("data/quip.cedarschema")).unwrap(),
        Extensions::all_available(),
    )
    .unwrap()
    .0;
    let schema = Schema::from_raw_schemafrag(fragment, SETTINGS.clone(), &mut u);

panics on the Schema::from_raw_schemafrag(fragment, SETTINGS.clone(), &mut u) call with the following stack trace

thread 'main' panicked at /Users/emcx/cedar-spec/cedar-policy-generators/src/schema.rs:731:18:
called `Result::unwrap()` on an `Err` value: TypeNotDefined(TypeNotDefinedError(NonEmpty { head: ConditionalName { possibilities: NonEmpty { head: InternalName { id: Id("String"), path: [], loc: Some(Loc { span: SourceSpan { offset: SourceOffset(79), length: 6 }, src: "// Entities\n// All enums are modeled as just String here\nentity User {\n    id: String,\n    name: String,\n};" }) }, tail: [] }, reference_type: CommonOrEntity, raw: RawName(InternalName { id: Id("String"), path: [], loc: Some(Loc { span: SourceSpan { offset: SourceOffset(79), length: 6 }, src: "// Entities\n// All enums are modeled as just String here\nentity User {\n    id: String,\n    name: String,\n};" }) }) }, tail: [] }))
stack backtrace:
   0: rust_begin_unwind
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/std/src/panicking.rs:665:5
   1: core::panicking::panic_fmt
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/core/src/panicking.rs:74:14
   2: core::result::unwrap_failed
             at /rustc/90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf/library/core/src/result.rs:1700:5
   3: core::result::Result<T,E>::unwrap
             at /Users/emcx/.rustup/toolchains/1.82.0-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:1104:23
   4: cedar_policy_generators::schema::Schema::from_raw_nsdef
             at /Users/emcx/cedar-spec/cedar-policy-generators/src/schema.rs:728:13
   5: cedar_policy_generators::schema::Schema::from_raw_schemafrag
             at /Users/emcx/cedar-spec/cedar-policy-generators/src/schema.rs:814:25
   6: avp_query_data_gen::main
             at ./src/main.rs:58:18
   7: core::ops::function::FnOnce::call_once
             at /Users/emcx/.rustup/toolchains/1.82.0-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5

Log output

// Put your output below this line

Additional configuration

No response

Operating System

No response

Additional information and screenshots

No response

@ericox ericox added bug Something isn't working pending-triage Hasn't been triaged yet labels Dec 17, 2024
@john-h-kastner-aws
Copy link
Contributor

Thanks for the report. I can reproduce this locally.

My current theory is that this was introduced by cedar-policy/cedar#1060 and specifically relates to the new EntityOrCommon variant, but I don't yet know what the appropriate fix is.

We didn't notice this ourselves because, as far as I can tell, this function is only use by the CLI for cedar-policy-generators which we don't currently use for anything.

@john-h-kastner-aws john-h-kastner-aws added pending-review Awaiting review by the core team and removed pending-triage Hasn't been triaged yet labels Dec 17, 2024
@shaobo-he-aws
Copy link
Contributor

I need to dive into it further but a preliminary analysis reveals that fully_qualify_type_references is invalid given cedar-policy/cedar#1060, since it can't be done until (validator-schema-)fragment-combine time, IIUC. We should remove these functions.

@cdisselkoen
Copy link
Contributor

Yes, there's a lot of logic at https://github.com/cedar-policy/cedar/blob/ed35ba7e2502c202a051e73e0bb8e4129244e73f/cedar-policy-validator/src/schema.rs#L382 that should probably be done in Schema::from_raw_schemafrag() but is currently not. I don't really want to copy-paste it in, it would be better to somehow encapsulate this logic into a reusable function that is called both there and in Schema::from_raw_schemafrag().

@cdisselkoen
Copy link
Contributor

Arguably it's a design flaw that cedar-policy-generators works on json_schema::NamespaceDefinition under the hood, rather than ValidatorSchema. Changing that would probably be the best resolution to this issue. Note that that's different from #75, that is, I'm talking about cedar_policy_validator::ValidatorSchema and not cedar_policy::Schema.

@shaobo-he-aws shaobo-he-aws removed the pending-review Awaiting review by the core team label Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants