Skip to content

Commit

Permalink
Fixes #429 - "toMatchGraphObjectSchema" fails when multiple classes s…
Browse files Browse the repository at this point in the history
…hare same "enum" property
  • Loading branch information
austinkelleher committed Mar 4, 2021
1 parent 967b572 commit 511b5da
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
48 changes: 45 additions & 3 deletions packages/integration-sdk-testing/src/jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,46 @@ function dedupSchemaPropertyTypes(
};
}

/**
* It's possible that two classes contain the same property name and both have
* "enum" values in the schema.
*
* For example, `Host` and `Device` both contain a `platform` property that
* contain enum values.
*
* See here:
*
* - https://github.com/JupiterOne/data-model/blob/master/src/schemas/Host.json#L75
* - https://github.com/JupiterOne/data-model/blob/master/src/schemas/Device.json#L58
*
* @param schema
*/
function dedupSchemaEnumValues(schema: GraphObjectSchema): GraphObjectSchema {
if (!schema.properties) {
return schema;
}

const newProperties: Record<string, any> = {};

for (const propertyName in schema.properties) {
const property = schema.properties[propertyName];

if (property.enum) {
newProperties[propertyName] = {
...property,
enum: Array.from(new Set(property.enum)),
};
} else {
newProperties[propertyName] = property;
}
}

return {
...schema,
properties: newProperties,
};
}

function generateGraphObjectSchemaFromDataModelSchemas(
schemas: GraphObjectSchema[],
) {
Expand Down Expand Up @@ -155,9 +195,11 @@ function generateGraphObjectSchemaFromDataModelSchemas(
newSchemas.push(schema);
}

return dedupSchemaRequiredPropertySchema(
dedupSchemaPropertyTypes(deepmerge.all(newSchemas)),
);
let resultSchema = dedupSchemaPropertyTypes(deepmerge.all(newSchemas));
resultSchema = dedupSchemaRequiredPropertySchema(resultSchema);
resultSchema = dedupSchemaEnumValues(resultSchema);

return resultSchema;
}

function graphObjectClassToSchemaRef(_class: string) {
Expand Down
6 changes: 6 additions & 0 deletions packages/integration-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to

## Unreleased

### Fixed

- [#429](https://github.com/JupiterOne/sdk/issues/429) - fixed
`toMatchGraphObjectSchema` failure when multiple classes share same `enum`
property name.

## 5.8.1 - 2021-03-03

### Fixed
Expand Down

0 comments on commit 511b5da

Please sign in to comment.