Skip to content

Commit

Permalink
Merge pull request #430 from JupiterOne/429-toMatchGraphObjectSchema-…
Browse files Browse the repository at this point in the history
…enum

Fixes #429 - "toMatchGraphObjectSchema" fails when multiple classes share same "enum" property
  • Loading branch information
austinkelleher authored Mar 4, 2021
2 parents 3afafa5 + f5fd12a commit ed0b4d8
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 22 deletions.
6 changes: 3 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jupiterone/cli",
"version": "5.8.1",
"version": "5.8.2",
"description": "The JupiterOne cli",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand All @@ -24,8 +24,8 @@
"test": "jest"
},
"dependencies": {
"@jupiterone/integration-sdk-core": "^5.8.1",
"@jupiterone/integration-sdk-runtime": "^5.8.1",
"@jupiterone/integration-sdk-core": "^5.8.2",
"@jupiterone/integration-sdk-runtime": "^5.8.2",
"@lifeomic/attempt": "^3.0.0",
"commander": "^5.0.0",
"globby": "^11.0.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/integration-sdk-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jupiterone/integration-sdk-cli",
"version": "5.8.1",
"version": "5.8.2",
"description": "The SDK for developing JupiterOne integrations",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand All @@ -22,7 +22,7 @@
"prepack": "yarn build:dist"
},
"dependencies": {
"@jupiterone/integration-sdk-runtime": "^5.8.1",
"@jupiterone/integration-sdk-runtime": "^5.8.2",
"commander": "^5.0.0",
"globby": "^11.0.0",
"lodash": "^4.17.19",
Expand All @@ -31,7 +31,7 @@
"vis": "^4.21.0-EOL"
},
"devDependencies": {
"@jupiterone/integration-sdk-private-test-utils": "^5.8.1",
"@jupiterone/integration-sdk-private-test-utils": "^5.8.2",
"@pollyjs/adapter-node-http": "^4.0.4",
"@pollyjs/core": "^4.0.4",
"@pollyjs/persister-fs": "^4.0.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/integration-sdk-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jupiterone/integration-sdk-core",
"version": "5.8.1",
"version": "5.8.2",
"description": "The SDK for developing JupiterOne integrations",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions packages/integration-sdk-dev-tools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jupiterone/integration-sdk-dev-tools",
"version": "5.8.1",
"version": "5.8.2",
"description": "A collection of developer tools that will assist with building integrations.",
"repository": "[email protected]:JupiterOne/sdk.git",
"author": "JupiterOne <[email protected]>",
Expand All @@ -15,8 +15,8 @@
"access": "public"
},
"dependencies": {
"@jupiterone/integration-sdk-cli": "^5.8.1",
"@jupiterone/integration-sdk-testing": "^5.8.1",
"@jupiterone/integration-sdk-cli": "^5.8.2",
"@jupiterone/integration-sdk-testing": "^5.8.2",
"@types/jest": "^25.2.3",
"@types/node": "^14.0.5",
"@typescript-eslint/eslint-plugin": "^3.8.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/integration-sdk-private-test-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@jupiterone/integration-sdk-private-test-utils",
"private": true,
"version": "5.8.1",
"version": "5.8.2",
"description": "The SDK for developing JupiterOne integrations",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -12,7 +12,7 @@
"node": "10.x || 12.x || 14.x"
},
"dependencies": {
"@jupiterone/integration-sdk-core": "^5.8.1",
"@jupiterone/integration-sdk-core": "^5.8.2",
"lodash": "^4.17.15",
"uuid": "^7.0.3"
},
Expand Down
6 changes: 3 additions & 3 deletions packages/integration-sdk-runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jupiterone/integration-sdk-runtime",
"version": "5.8.1",
"version": "5.8.2",
"description": "The SDK for developing JupiterOne integrations",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand All @@ -24,7 +24,7 @@
"prepack": "yarn build:dist"
},
"dependencies": {
"@jupiterone/integration-sdk-core": "^5.8.1",
"@jupiterone/integration-sdk-core": "^5.8.2",
"@lifeomic/alpha": "^1.4.0",
"@lifeomic/attempt": "^3.0.0",
"async-sema": "^3.1.0",
Expand All @@ -43,7 +43,7 @@
"uuid": "^7.0.3"
},
"devDependencies": {
"@jupiterone/integration-sdk-private-test-utils": "^5.8.1",
"@jupiterone/integration-sdk-private-test-utils": "^5.8.2",
"@types/uuid": "^7.0.2",
"get-port": "^5.1.1",
"memfs": "^3.2.0",
Expand Down
8 changes: 4 additions & 4 deletions packages/integration-sdk-testing/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jupiterone/integration-sdk-testing",
"version": "5.8.1",
"version": "5.8.2",
"description": "Testing utilities for JupiterOne integrations",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand All @@ -23,8 +23,8 @@
"prepack": "yarn build:dist"
},
"dependencies": {
"@jupiterone/integration-sdk-core": "^5.8.1",
"@jupiterone/integration-sdk-runtime": "^5.8.1",
"@jupiterone/integration-sdk-core": "^5.8.2",
"@jupiterone/integration-sdk-runtime": "^5.8.2",
"@pollyjs/adapter-node-http": "^4.0.4",
"@pollyjs/core": "^4.0.4",
"@pollyjs/persister-fs": "^4.0.4",
Expand All @@ -36,7 +36,7 @@
"lodash": "^4.17.15"
},
"devDependencies": {
"@jupiterone/integration-sdk-private-test-utils": "^5.8.1",
"@jupiterone/integration-sdk-private-test-utils": "^5.8.2",
"@types/lodash": "^4.14.149",
"get-port": "^5.1.1",
"memfs": "^3.2.0"
Expand Down
34 changes: 34 additions & 0 deletions packages/integration-sdk-testing/src/__tests__/jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,40 @@ Find out more about JupiterOne schemas: https://github.com/JupiterOne/data-model
)`,
);
});

test('should try to merge two enum set properties in array of classes', () => {
const entity: Entity = {
_class: ['Host', 'Device'],
_type: 'user_endpoint',
_key: 'enum-set-test',
name: 'test',
displayName: 'test',
category: 'endpoint',
make: 'Apple',
model: 'MacBookPro14,3',
serial: 'C0123',
platform: 'darwin',
specialProp: 'abc',
};

const result = toMatchGraphObjectSchema(entity, {
_class: ['Host', 'Device'],
schema: {
additionalProperties: false,
properties: {
_type: { const: 'user_endpoint' },
specialProp: { type: 'string' },
},
},
});

expect(result).toEqual({
message: expect.any(Function),
pass: true,
});

expect(result.message()).toEqual('Success!');
});
});

describe('#toMatchDirectRelationshipSchema', () => {
Expand Down
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
8 changes: 8 additions & 0 deletions packages/integration-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ and this project adheres to

## Unreleased

## 5.8.2 - 2021-03-04

### 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 ed0b4d8

Please sign in to comment.