Skip to content

Commit 65779fb

Browse files
committed
Exclude associations by default
Allow strategy to reject additional properties
1 parent 54abad5 commit 65779fb

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ oapi.schema('User', schemaManager.generate(UserModel, strategy))
2828

2929
## Configuration Options
3030

31+
Starting from 1.0.0, additional properties can be rejected
32+
33+
```typescript
34+
const strategy = new OpenApiStrategy({ additionalProperties: false })
35+
```
36+
3137
Pass (per) model options to the generate() method:
3238

3339
```typescript
@@ -44,6 +50,8 @@ const userSchema = schemaManager.generate(userModel, strategy, {
4450

4551
`jsonSchema` and `schema` works the same as sequelize-to-json-schemas
4652

53+
Starting from 1.0.0, associations are excluded by default
54+
4755
## Unsupported Types
4856

4957
* GEOMETRY

src/schema-manager.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { pick, omit } from './utils/util'
1010
export type ModelContainer = {
1111
type: string,
1212
properties?: {}
13-
required?: string[]
13+
required?: string[],
14+
additionalProperties: boolean
1415
}
1516

1617
export type ModelOptions = {
@@ -39,7 +40,7 @@ export default class SchemaManager {
3940
{
4041
include: [],
4142
exclude: [],
42-
associations: true,
43+
associations: false,
4344
includeAssociations: [],
4445
excludeAssociations: [],
4546
},
@@ -137,7 +138,8 @@ export default class SchemaManager {
137138
const result: ModelContainer = {
138139
// identical for all models and schemas thus no need to over-engineer
139140
type: 'object',
140-
properties: {}
141+
properties: {},
142+
additionalProperties: _strategy.get(this).additionalProperties
141143
}
142144

143145
return result

src/strategies/openapi.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import { Association, ModelValidateOptions } from 'sequelize'
22

33

44
export default class OpenApiStrategy {
5+
public additionalProperties: boolean
6+
7+
constructor(options: { additionalProperties?: boolean } = {}) {
8+
this.additionalProperties = options.additionalProperties || false
9+
}
10+
511
getPropertyExamples(examples: any[]) {
612
return {
713
example: examples

test/openapi-strategy.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ describe('OpenApi3Strategy', function () {
2121
// generate schema
2222
// ------------------------------------------------------------------------
2323
const schemaManager = new SchemaManager
24-
const strategy = new OpenApiStrategy
25-
const schema = schemaManager.generate(models.User, strategy)
24+
const strategy = new OpenApiStrategy({ additionalProperties: true })
25+
const schema = schemaManager.generate(models.User, strategy, { associations: true })
2626

2727
// validate document using ajv
2828
const ajv = new Ajv()
@@ -629,5 +629,9 @@ describe('OpenApi3Strategy', function () {
629629
})
630630
})
631631
})
632+
633+
describe("Ensure OpenApiStrategy allows 'additionalProperties'", function () {
634+
expect(schema.additionalProperties).toEqual(true)
635+
})
632636
})
633637
})

test/schema-manager.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ describe('SchemaManager', function() {
4949
// ------------------------------------------------------------------------
5050
// make sure option 'associations' functions as expected
5151
// ------------------------------------------------------------------------
52-
describe(`Ensure option 'associations' with default value 'true':`, function () {
52+
describe(`Ensure option 'associations' with user-specificed value 'true':`, function () {
5353
const schemaManager = new SchemaManager
5454
const strategy = new OpenApiStrategy
55-
const schema = schemaManager.generate(models.User, strategy)
55+
const schema = schemaManager.generate(models.User, strategy, { associations: true })
5656

5757
it(`generates association property 'profile'`, function () {
5858
expect(schema.properties).toHaveProperty('profile')
@@ -63,12 +63,10 @@ describe('SchemaManager', function() {
6363
})
6464
})
6565

66-
describe(`Ensure option 'associations' with user-specificed value 'false':`, function () {
66+
describe(`Ensure option 'associations' with default value 'false':`, function () {
6767
const schemaManager = new SchemaManager
6868
const strategy = new OpenApiStrategy
69-
const schema = schemaManager.generate(models.User, strategy, {
70-
associations: false,
71-
})
69+
const schema = schemaManager.generate(models.User, strategy)
7270

7371
it(`does not generate association property 'profile'`, function () {
7472
expect(schema.properties!['profile']).toBeUndefined()
@@ -88,6 +86,7 @@ describe('SchemaManager', function() {
8886
const strategy = new OpenApiStrategy
8987
const schema = schemaManager.generate(models.User, strategy, {
9088
includeAssociations: ['profile'],
89+
associations: true
9190
});
9291

9392
it(`include association 'profile'`, function () {
@@ -107,6 +106,7 @@ describe('SchemaManager', function() {
107106
const strategy = new OpenApiStrategy
108107
const schema = schemaManager.generate(models.User, strategy, {
109108
excludeAssociations: ['profile'],
109+
associations: true
110110
});
111111

112112
it(`do not include association 'profile'`, function () {

0 commit comments

Comments
 (0)