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

FHIR primitive static factory helpers should throw appropriate exceptions when null is passed #1814

Closed
lmsurpre opened this issue Dec 11, 2020 · 2 comments
Assignees
Labels
enhancement New feature or request P3 Priority 3 - Nice To Have

Comments

@lmsurpre
Copy link
Member

lmsurpre commented Dec 11, 2020

Is your feature request related to a problem? Please describe.
When populating an object from a different source, its convenient to use the static factory methods like Code.of() like the following

Coding.builder()
    .code(Code.of(myObj.getMyCode()))
    .display(string("Pregnant"))
    .build()

However, if that source data can contain nulls, then today this breaks on the Code.of(null) with the following:

java.lang.IllegalStateException: ele-1: All FHIR elements must have a @value or children

This despite the following code working with no issue:

Coding.builder()
    .code(null)
    .display(string("Pregnant"))
    .build()

Describe the solution you'd like
Code.of(null) and similar convience functions should return null if null is passed in. Instead of trying to create a privitive FHIR object with no value which is guaranteed to fail with ele-1.

Describe alternatives you've considered
Add javadoc to explain it will throw if you pass a null.

@lmsurpre
Copy link
Member Author

Team decision:

  1. update javadoc to say that the arguments to the factory methods must be non-null
  2. add Object.requireNonNull to all the generated factory methods (with the name of the argument so we know what was null)

@lmsurpre lmsurpre added the P3 Priority 3 - Nice To Have label Feb 22, 2021
lmsurpre added a commit that referenced this issue Apr 25, 2021
Signed-off-by: Lee Surprenant <lmsurpre@us.ibm.com>
lmsurpre added a commit that referenced this issue Apr 26, 2021
Signed-off-by: Lee Surprenant <lmsurpre@us.ibm.com>
@lmsurpre lmsurpre self-assigned this Apr 28, 2021
@michaelwschroeder
Copy link
Contributor

I have validated the javadoc and the addition of Object.requireNotNull() method calls via visual inspection of classes in the fhir-model project and have validated that when attempting to pass a null value to these factory methods a NullPointerException is thrown indicating the name of the argument containing the null value.

@lmsurpre lmsurpre changed the title FHIR primitive static factory helpers should be more resilient to nulls FHIR primitive static factory helpers should throw appropriate exceptions when null is passed May 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request P3 Priority 3 - Nice To Have
Projects
None yet
Development

No branches or pull requests

3 participants