Skip to content

Commit

Permalink
Update display for resources without id or resourceType (#151)
Browse files Browse the repository at this point in the history
* Update display for resources without id or resourceType

- If a LogicalModel example does not have id, fall back to using
  the resourceType (or a portion of the resourceType) if possible.
- If there is no resouceType or id, then use 'Untitled'

* Include 'Instance of' for resources without an id to more clearly identify them
  • Loading branch information
jafeltra authored Feb 7, 2024
1 parent 3848457 commit 9179f3d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/components/JSONOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ const checkFshType = (def) => {
return null;
};

const checkIdToDisplay = (def) => {
if (def.id) {
return def.id;
}
if (def.resourceType) {
// Logical Model instances don't always have an id, so fall back to last part of the resourceType
// All other definitions are StructureDefinitions/ValueSets/CodeSystems, which are guaranteed to have id
// so the only time this case is reached is through Logical Model Instances or someone typing/removing an id
return `Instance of ${def.resourceType.substring(def.resourceType.lastIndexOf('/') + 1)}`;
}
// If can't determine an id or a best guess from the resourceType, just return 'Untitled'
return 'Untitled';
};

// Flatten the package so we can render and navigate it more easily,
// but keep high level attributes we'll need accessible
const getIterablePackage = (defsPackage) => {
Expand All @@ -126,7 +140,11 @@ const getIterablePackage = (defsPackage) => {
...defsPackage.valueSets,
...defsPackage.codeSystems
];
return defArray.map((def) => ({ resourceType: checkFshType(def), id: def.id, def: JSON.stringify(def, null, 2) }));
return defArray.map((def) => ({
resourceType: checkFshType(def),
id: checkIdToDisplay(def),
def: JSON.stringify(def, null, 2)
}));
};

export default function JSONOutput(props) {
Expand Down Expand Up @@ -189,7 +207,7 @@ export default function JSONOutput(props) {
if (!fhirDefinitions[currentDef]) {
updatedDefs[currentDef] = {
resourceType: checkFshType(latestJSON),
id: latestJSON.id ?? 'Untitled'
id: checkIdToDisplay(latestJSON)
};
}

Expand All @@ -200,7 +218,7 @@ export default function JSONOutput(props) {

// Update id if it has changed or it is new
if (!fhirDefinitions[currentDef] || latestJSON.id !== fhirDefinitions[currentDef].id) {
updatedDefs[currentDef].id = latestJSON.id ?? 'Untitled';
updatedDefs[currentDef].id = checkIdToDisplay(latestJSON);
}
} catch (e) {
// Invalid JSON typed. Keep track of index.
Expand Down
12 changes: 11 additions & 1 deletion src/tests/components/JSONOutput.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,13 @@ describe('file tree display', () => {
profiles: [
{
resourceType: 'StructureDefinition'
// No id field - this is to represent the case when someone removed or never included an id. SUSHI packages should always have ids.
// No id field - this is to represent the case when someone removed or never included an id or a Logical Model without an id.
},
{
resourceType: 'http://example.org/StructureDefinition/example-logical-model'
},
{
other: 'not a resource type'
}
],
extensions: [],
Expand All @@ -388,6 +394,10 @@ describe('file tree display', () => {
container
);

const structureDef = getByText('Instance of StructureDefinition');
expect(structureDef).toBeInTheDocument();
const exampleLM = getByText('Instance of example-logical-model');
expect(exampleLM).toBeInTheDocument();
const untitledDef = getByText('Untitled');
expect(untitledDef).toBeInTheDocument();
});
Expand Down

0 comments on commit 9179f3d

Please sign in to comment.