Skip to content

Commit

Permalink
Fix: now renders valid OAS3 for Sequelize BLOB DataType (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
bravo-kernel committed Nov 28, 2020
1 parent 24ecfa2 commit c05477f
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 5 deletions.
14 changes: 13 additions & 1 deletion examples/json-schema-v7.md
@@ -1,6 +1,6 @@
# JSON Schema Draft-07

These schemas were automatically generated on 2020-01-25
These schemas were automatically generated on 2020-11-28
using [these Sequelize models](../test/models) and the most recent version of
sequelize-to-json-schemas. To confirm that these are indeed all valid schemas use:

Expand Down Expand Up @@ -68,6 +68,11 @@ sequelize-to-json-schemas. To confirm that these are indeed all valid schemas us
"type": "string"
}
},
"BLOB": {
"$id": "https://api.example.com/properties/BLOB",
"type": "string",
"contentEncoding": "base64"
},
"CITEXT": {
"$id": "https://api.example.com/properties/CITEXT",
"type": "string"
Expand Down Expand Up @@ -259,6 +264,7 @@ sequelize-to-json-schemas. To confirm that these are indeed all valid schemas us
"updatedAt",
"ARRAY_INTEGERS",
"ARRAY_TEXTS",
"BLOB",
"CITEXT",
"INTEGER",
"STRING",
Expand Down Expand Up @@ -495,6 +501,11 @@ document (by adding model schemas to `definitions`).
"type": "string"
}
},
"BLOB": {
"$id": "https://api.example.com/properties/BLOB",
"type": "string",
"contentEncoding": "base64"
},
"CITEXT": {
"$id": "https://api.example.com/properties/CITEXT",
"type": "string"
Expand Down Expand Up @@ -686,6 +697,7 @@ document (by adding model schemas to `definitions`).
"updatedAt",
"ARRAY_INTEGERS",
"ARRAY_TEXTS",
"BLOB",
"CITEXT",
"INTEGER",
"STRING",
Expand Down
12 changes: 11 additions & 1 deletion examples/openapi-v3.md
@@ -1,6 +1,6 @@
# OpenAPI 3.0

These schemas were automatically generated on 2020-01-25
These schemas were automatically generated on 2020-11-28
using [these Sequelize models](../test/models) and the most recent version of
sequelize-to-json-schemas. To confirm that these are indeed all valid schemas use:

Expand Down Expand Up @@ -56,6 +56,10 @@ sequelize-to-json-schemas. To confirm that these are indeed all valid schemas us
},
"nullable": true
},
"BLOB": {
"type": "string",
"format": "byte"
},
"CITEXT": {
"type": "string"
},
Expand Down Expand Up @@ -216,6 +220,7 @@ sequelize-to-json-schemas. To confirm that these are indeed all valid schemas us
"updatedAt",
"ARRAY_INTEGERS",
"ARRAY_TEXTS",
"BLOB",
"CITEXT",
"INTEGER",
"STRING",
Expand Down Expand Up @@ -421,6 +426,10 @@ example of how to integrate the generated model schemas into a full OpenAPI 3.0
},
"nullable": true
},
"BLOB": {
"type": "string",
"format": "byte"
},
"CITEXT": {
"type": "string"
},
Expand Down Expand Up @@ -581,6 +590,7 @@ example of how to integrate the generated model schemas into a full OpenAPI 3.0
"updatedAt",
"ARRAY_INTEGERS",
"ARRAY_TEXTS",
"BLOB",
"CITEXT",
"INTEGER",
"STRING",
Expand Down
16 changes: 16 additions & 0 deletions lib/strategies/json-schema-v7.js
Expand Up @@ -99,6 +99,22 @@ class JsonSchema7Strategy extends StrategyInterface {
};
}

/**
* Returns the `contentEncoding` property as used by Json Schema for base64 encoded strings (like BLOB).
*
* @example
* {
* 'contentEncoding': 'base64',
* }
*
* @returns {object}
*/
getPropertyForBase64Encoding() {
return {
contentEncoding: 'base64',
};
}

/**
* Returns the property pointing to a HasOne association.
*
Expand Down
16 changes: 16 additions & 0 deletions lib/strategies/openapi-v3.js
Expand Up @@ -61,6 +61,22 @@ class OpenApi3Strategy extends StrategyInterface {
};
}

/**
* Returns the `format` property as used by OAS for base64 base64 encoded strings (like BLOB).
*
* @example
* {
* 'format': 'byte',
* }
*
* @returns {object}
*/
getPropertyForBase64Encoding() {
return {
format: 'byte',
};
}

/**
* Returns a new `type` property, enriched to allow null values.
*
Expand Down
12 changes: 12 additions & 0 deletions lib/strategy-interface.js
Expand Up @@ -54,6 +54,18 @@ class StrategyInterface {
this.constructor._throwMissingImplementationError(this.constructor.name, 'getPropertyExamples');
}

/**
* Must return the strategy specific property used for base64 string encoding.
*
* @returns {object}
*/
getPropertyForBase64Encoding() {
this.constructor._throwMissingImplementationError(
this.constructor.name,
'getPropertyForBase64Encoding',
);
}

/**
* Must returns a new `type` object, enriched to allow null values.
*
Expand Down
3 changes: 2 additions & 1 deletion lib/type-mapper.js
Expand Up @@ -76,7 +76,8 @@ class TypeMapper {
// @todo BLOB('tiny')
// ----------------------------------------------------------------------
case 'BLOB': {
result = { ...STRING, contentEncoding: 'base64' };
result = { ...STRING };
Object.assign(result, strategy.getPropertyForBase64Encoding());
break;
}

Expand Down
7 changes: 7 additions & 0 deletions test/models/user.js
Expand Up @@ -57,6 +57,13 @@ module.exports = (sequelize, { DataTypes }) => {
};
}

if (supportedDataType('BLOB')) {
Model.rawAttributes.BLOB = {
type: DataTypes.BLOB,
allowNull: false,
};
}

if (supportedDataType('CITEXT')) {
Model.rawAttributes.CITEXT = {
type: DataTypes.CITEXT,
Expand Down
6 changes: 6 additions & 0 deletions test/strategies/json-schema-v7-strategy.test.js
Expand Up @@ -40,6 +40,12 @@ describe('JsonSchema7Strategy', function () {
// make sure sequelize DataTypes render as expected
// ------------------------------------------------------------------------
describe('Ensure Sequelize DataTypes are properly converted and thus:', function () {
describe('BLOB', function () {
it("has property 'contentEncoding' of type 'base64'", function () {
expect(schema.properties.BLOB.contentEncoding).toEqual('base64');
});
});

describe('STRING_ALLOWNULL_EXPLICIT', function () {
it("has property 'type' of type 'array'", function () {
expect(Array.isArray(schema.properties.STRING_ALLOWNULL_EXPLICIT.type)).toBe(true);
Expand Down
6 changes: 6 additions & 0 deletions test/strategies/openapi-v3-stragegy.test.js
Expand Up @@ -26,6 +26,12 @@ describe('OpenApi3Strategy', function () {
// make sure sequelize DataTypes render as expected
// ------------------------------------------------------------------------
describe('Ensure Sequelize DataTypes are properly converted and thus:', function () {
describe('BLOB', function () {
it("has property 'format' of type 'byte'", function () {
expect(schema.properties.BLOB.format).toEqual('byte');
});
});

describe('STRING_ALLOWNULL_EXPLICIT', function () {
it("has property 'type' of type 'string'", function () {
expect(schema.properties.STRING_ALLOWNULL_EXPLICIT.type).toEqual('string');
Expand Down
12 changes: 10 additions & 2 deletions test/strategy-interface.test.js
Expand Up @@ -17,8 +17,8 @@ describe('StrategyInterface', function () {
describe('Ensure we are testing against:', function () {
const methodCount = Object.getOwnPropertyNames(StrategyInterface.prototype).length - 1; // excluding the constructor

it(`7 interface methods`, function () {
expect(methodCount).toEqual(7);
it(`8 interface methods`, function () {
expect(methodCount).toEqual(8);
});
});

Expand Down Expand Up @@ -56,6 +56,14 @@ describe('StrategyInterface', function () {
}).toThrow("DummyStrategy has not implemented the 'getPropertyExamples' interface method.");
});

it('getPropertyForBase64Encoding()', function () {
expect(() => {
dummyStrategy.getPropertyForBase64Encoding();
}).toThrow(
"DummyStrategy has not implemented the 'getPropertyForBase64Encoding' interface method.",
);
});

it('convertTypePropertyToAllowNull()', function () {
expect(() => {
dummyStrategy.convertTypePropertyToAllowNull();
Expand Down

0 comments on commit c05477f

Please sign in to comment.