Skip to content

Commit

Permalink
add "through" support for belongsToMany associations
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfgangwalther committed Nov 6, 2019
1 parent af34047 commit 4e54958
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 8 deletions.
28 changes: 26 additions & 2 deletions lib/strategies/json-schema-v7.js
Expand Up @@ -170,7 +170,19 @@ class JsonSchema7Strategy extends StrategyInterface {
* friends: {
* type: "array",
* items: {
* $ref: '#/definitions/user'
* allOf: [
* {
* $ref: '#/definitions/user'
* },
* {
* type: 'object',
* properties: {
* friendship: {
* $ref: '#/definitions/friendship'
* }
* }
* }
* ]
* }
* }
* }
Expand All @@ -183,7 +195,19 @@ class JsonSchema7Strategy extends StrategyInterface {
[associationName]: {
type: 'array',
items: {
$ref: `#/definitions/${association.target.name}`, // eslint-disable-line unicorn/prevent-abbreviations
allOf: [
{
$ref: `#/definitions/${association.target.name}`, // eslint-disable-line unicorn/prevent-abbreviations
},
{
type: 'object',
properties: {
[association.through.model.options.name.plural]: {
$ref: `#/definitions/${association.through.model.name}`,
},
},
},
],
},
},
};
Expand Down
28 changes: 26 additions & 2 deletions lib/strategies/openapi-v3.js
Expand Up @@ -156,7 +156,19 @@ class OpenApi3Strategy extends StrategyInterface {
* friends: {
* type: "array",
* items: {
* $ref: '#/components/schemas/user'
* allOf: [
* {
* $ref: '#/components/schemas/user'
* },
* {
* type: 'object',
* properties: {
* friendship: {
* $ref: '#/components/schemas/friendship'
* }
* }
* }
* ]
* }
* }
* }
Expand All @@ -169,7 +181,19 @@ class OpenApi3Strategy extends StrategyInterface {
[associationName]: {
type: 'array',
items: {
$ref: `#/components/schemas/${association.target.name}`, // eslint-disable-line unicorn/prevent-abbreviations
allOf: [
{
$ref: `#/components/schemas/${association.target.name}`, // eslint-disable-line unicorn/prevent-abbreviations
},
{
type: 'object',
properties: {
[association.through.model.options.name.plural]: {
$ref: `#/components/schemas/${association.through.model.name}`,
},
},
},
],
},
},
};
Expand Down
19 changes: 17 additions & 2 deletions test/strategies/json-schema-v7-strategy.test.js
Expand Up @@ -93,11 +93,26 @@ describe('JsonSchema7Strategy', function() {
expect(schema.properties.friends.type).toEqual('array');
});

it("array 'items' holding an object with '$ref' pointing at '#/definitions/user'", function() {
expect(schema.properties.friends.items).toEqual({
it("property 'items.allOf' of type 'array'", function() {
expect(Array.isArray(schema.properties.friends.items.allOf)).toBe(true);
});

it("array 'items.allOf' holding an object with '$ref' pointing at '#/definitions/user'", function() {
expect(schema.properties.friends.items.allOf[0]).toEqual({
$ref: '#/definitions/user', // eslint-disable-line unicorn/prevent-abbreviations
});
});

it("array 'items.allOf' holding an object with type object and properties.friendships an object with '$ref' pointing at '#/definitions/friendship'", function() {
expect(schema.properties.friends.items.allOf[1]).toEqual({
type: 'object',
properties: {
friendships: {
$ref: '#/definitions/friendship', // eslint-disable-line unicorn/prevent-abbreviations
},
},
});
});
});
});

Expand Down
19 changes: 17 additions & 2 deletions test/strategies/openapi-v3-stragegy.test.js
Expand Up @@ -114,11 +114,26 @@ describe('OpenApi3Strategy', function() {
expect(schema.properties.friends.type).toEqual('array');
});

it("array 'items' holding an object with '$ref' pointing to '#/components/schemas/user'", function() {
expect(schema.properties.friends.items).toEqual({
it("property 'items.allOf' of type 'array'", function() {
expect(Array.isArray(schema.properties.friends.items.allOf)).toBe(true);
});

it("array 'items.allOf' holding an object with '$ref' pointing to '#/components/schemas/user'", function() {
expect(schema.properties.friends.items.allOf[0]).toEqual({
$ref: '#/components/schemas/user', // eslint-disable-line unicorn/prevent-abbreviations
});
});

it("array 'items.allOf' holding an object with type object and properties.friendships an object with '$ref' pointing at '#/components/schemas/friendship'", function() {
expect(schema.properties.friends.items.allOf[1]).toEqual({
type: 'object',
properties: {
friendships: {
$ref: '#/components/schemas/friendship', // eslint-disable-line unicorn/prevent-abbreviations
},
},
});
});
});
});

Expand Down

0 comments on commit 4e54958

Please sign in to comment.