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

Consider constraining allowed types of input to getReferenceString #4378

Open
josh- opened this issue Apr 16, 2024 · 7 comments
Open

Consider constraining allowed types of input to getReferenceString #4378

josh- opened this issue Apr 16, 2024 · 7 comments
Labels
v4 Changes, often breaking, to consider for the next major release, Medplum v4

Comments

@josh-
Copy link
Contributor

josh- commented Apr 16, 2024

Currently getReferenceString allows an input of Resource | Reference. However because all the properties on Reference are optional, this function allows any object to be passed in.

For example, the following TypeScript code compiles without error:

const string = getReferenceString({});
console.log('reference string is', string);

and prints the following:

reference string is undefined/undefined

this tripped us up in production recently.


would you consider potentially changing the signature of this function to enforce the string value being present on the passed-in input? for example something like so?

export function getReferenceString(
  input:
    | (Reference & { reference: string })
    | (Resource & { resourceType: ResourceType; id: string }),
): string {
  if (isReference(input)) {
    return input.reference;
  }
  return `${input.resourceType}/${input.id}`;
}

this would result in the above code sample no longer compiling successfully:

const string = getReferenceString({});
error TS2345: Argument of type '{}' is not assignable to parameter of type '(Reference<Resource> & { reference: string; }) | (Resource & { resourceType: "Appointment" | "AccessPolicy" | "Account" | "ActivityDefinition" | ... 157 more ... | "VisionPrescription"; id: string; })'.
@ThatOneBro
Copy link
Member

I think this is a sane change to the signature, and would definitely prevent a lot of easy to prevent bugs. I'm all for it

What do you think @codyebberson ?

@codyebberson
Copy link
Member

Sounds good in principle. Might be consider a breaking change, but agreed that it's for the better. We should try it on the medplum monorepo and see what breaks.

@codyebberson
Copy link
Member

This causes quite a few build errors in our monorepo. They are all fixable, but I'm a little nervous that Medplum customers will need to make a ton of changes on next upgrade.

Maybe a v4 change?

Should getReferenceString() throw when there is not a valid reference string?

@josh-
Copy link
Contributor Author

josh- commented Apr 17, 2024

I think it makes sense for getReferenceString to throw when it will be producing an invalid string - and if this is done including in v4 sounds good to me.

@josh-
Copy link
Contributor Author

josh- commented Apr 17, 2024

Also thinking about this a bit more - we haven't yet enabled checkReferencesOnWrite (we are planning to), but I assume having that enabled would have caught this - wondering if you have any plans to enable this by default on new projects?

@rahul1 rahul1 added the v4 Changes, often breaking, to consider for the next major release, Medplum v4 label Apr 30, 2024
@codyebberson
Copy link
Member

wondering if you have any plans to enable this by default on new projects?

At present, there is a somewhat severe performance penalty for enabling this feature, so we do not plan to enable by default.

@rahul1 rahul1 added this to the Milestone Quality milestone Apr 30, 2024
@josh-
Copy link
Contributor Author

josh- commented May 1, 2024

No worries - makes sense 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v4 Changes, often breaking, to consider for the next major release, Medplum v4
Projects
Status: No status
Development

No branches or pull requests

4 participants