Skip to content

Commit 5bf209c

Browse files
committed
feat!: rename sanitizeSchema to transformSchema and adjust refs in default implementation
1 parent 5aa4c24 commit 5bf209c

File tree

11 files changed

+359
-254
lines changed

11 files changed

+359
-254
lines changed

docs/api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ __Options:__
167167
- Provide any schema that has an `$id` with a ref as key, check `service.docs.refs` for allowed refs
168168
- This will add the schema and use it for the given ref
169169
- `docs` - Any service.docs options that will be merged into the resulting object and would overwrite anything that will be generated
170-
- `sanitizeSchema` - A function that sanitizes the schema from properties that are not allowed in an OpenApi specification.
171-
If not provided a default implementation will be used.
170+
- `transformSchema` - A function that transforms the schema to a valid OpenApi schema.
171+
If not provided a default implementation will be used which removes unsupported properties and adjust references.
172172

173173
### Path support to update nested structures
174174

docs/examples/authentication_v5.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
### Example with feathers generated authentication on feathersjs v5 (dove) <!-- {docsify-ignore} -->
22

33
1. When you have generated the app with authentication you have to add some things to the initial
4-
specs when registering feathers swagger.
4+
specs when registering feathers-swagger.
55

66
```typescript
77
app.configure(

docs/examples/generated_service_v5.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,35 @@
22

33
1. Go into your `src/services/{$name}` folder, and open the service you want to edit `${name}.[tj]s`
44
2. Import the helper function and import the schemas (example for user service):
5-
```js {"highlight": "9-12", "lineNumbers": true}
5+
```js {"highlight": "11-14", "lineNumbers": true}
66
import { createSwaggerServiceOptions } from 'feathers-swagger';
77
import {
8-
userDataValidator,
9-
userQueryValidator,
10-
userResolver,
11-
userDataResolver,
12-
userQueryResolver,
13-
userExternalResolver,
14-
userDataSchema,
15-
userQuerySchema,
16-
userSchema,
17-
userPatchSchema,
8+
userDataValidator,
9+
userPatchValidator,
10+
userQueryValidator,
11+
userResolver,
12+
userExternalResolver,
13+
userDataResolver,
14+
userPatchResolver,
15+
userQueryResolver,
16+
userSchema,
17+
userDataSchema,
18+
userPatchSchema,
19+
userQuerySchema
1820
} from './users.schema';
1921
```
2022
adjust the options when the service is generated
2123
```js {"highlight": "6-12", "lineNumbers": true}
2224
app.use('users', new UserService(getOptions(app)), {
2325
// A list of all methods this service exposes externally
24-
methods: ['find', 'get', 'create', 'update', 'patch', 'remove'],
26+
methods: userMethods,
2527
// You can add additional custom events to be sent to clients here
2628
events: [],
2729
docs: createSwaggerServiceOptions({
28-
schemas: { userDataSchema, userQuerySchema, userSchema, userPatchSchema },
30+
schemas: { userSchema, userDataSchema, userPatchSchema, userQuerySchema },
2931
docs: {
3032
// any options for service.docs can be added here
31-
securities: ['find', 'get', 'update', 'patch', 'remove'],
33+
securities: ['find', 'get', 'patch', 'remove'],
3234
}
3335
}),
3436
});

docs/examples/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
In addition to the examples you can access from the navigation, you can check out the [example application](https://github.com/feathersjs-ecosystem/feathers-swagger/tree/{GITHUB_BRANCH}/example) of the git repository.
44

5+
There is also a [repository with examples of integrations of feathers-swagger](https://github.com/Mairu/feathersjs-swagger-tests). It contains multiple branches for different versions and configurations.
6+
57
It contains many configurations and can be start with `npm start` when feathers-swagger have been checked out.
68

79
### List of examples <!-- {docsify-ignore} -->

docs/getting-started.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,24 @@ The docs property can be set as [path service options](https://dove.feathersjs.c
131131
```typescript
132132
import type { createSwaggerServiceOptions } from 'feathers-swagger';
133133
import {
134-
messageDataSchema,
135-
messageQuerySchema,
134+
// ...
136135
messageSchema,
137-
} from './message.schema';
136+
messageDataSchema,
137+
messagePatchSchema,
138+
messageQuerySchema
139+
} from './messages.schema';
138140

139141
// ...
140142

141143
app.use('message', new MessageService(), {
142-
methods: ['find', 'get', 'create', 'update', 'patch', 'remove'],
144+
methods: messageMethods,
143145
events: [],
144146
docs: createSwaggerServiceOptions({
145147
schemas: { messageDataSchema, messageQuerySchema, messageSchema },
146-
docs: { description: 'My custom service description' }
148+
docs: {
149+
description: 'My custom service description',
150+
securities: ['all'],
151+
}
147152
})
148153
});
149154
```

lib/utils.js

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ const allowedDefaultRefs = [
7272
'queryParameters'
7373
];
7474

75-
// Remove non OpenApi properties (properties from https://swagger.io/specification/#schema-object)
76-
exports.defaultSanitizeSchema = function defaultSanitizeSchema (schema) {
77-
return pick(schema, [
75+
exports.defaultTransformSchema = function defaultTransformSchema (schema) {
76+
const allowedProperties = pick(schema, [
7877
'title',
7978
'multipleOf',
8079
'maximum',
@@ -90,6 +89,8 @@ exports.defaultSanitizeSchema = function defaultSanitizeSchema (schema) {
9089
'maxProperties',
9190
'minProperties',
9291
'required',
92+
'dependentRequired',
93+
'const',
9394
'enum',
9495
'type',
9596
'allOf',
@@ -109,8 +110,38 @@ exports.defaultSanitizeSchema = function defaultSanitizeSchema (schema) {
109110
'xml',
110111
'externalDocs',
111112
'example',
112-
'deprecated'
113+
'deprecated',
114+
'$ref',
115+
'$dynamicRef',
116+
'$anchor',
117+
'$dynamicAnchor',
118+
'$recursiveRef',
119+
'$recursiveAnchor',
120+
'$defs',
121+
'$comment'
113122
]);
123+
124+
if (allowedProperties.$ref && !allowedProperties.$ref.includes('#')) {
125+
allowedProperties.$ref = '#/components/schemas/' + allowedProperties.$ref;
126+
}
127+
128+
if (allowedProperties.items) {
129+
allowedProperties.items = defaultTransformSchema(allowedProperties.items);
130+
}
131+
132+
if (allowedProperties.properties) {
133+
allowedProperties.properties = Object.entries(allowedProperties.properties).reduce(
134+
(previousValue, [key, value]) => {
135+
return {
136+
...previousValue,
137+
[key]: defaultTransformSchema(value)
138+
};
139+
},
140+
{}
141+
);
142+
}
143+
144+
return allowedProperties;
114145
};
115146

116147
function determineSchemaPrefix (schemas) {
@@ -134,9 +165,9 @@ function determineSchemaPrefix (schemas) {
134165
return undefined;
135166
}
136167

137-
exports.createSwaggerServiceOptions = function createSwaggerServiceOptions ({ schemas, docs, sanitizeSchema }) {
168+
exports.createSwaggerServiceOptions = function createSwaggerServiceOptions ({ schemas, docs, transformSchema }) {
138169
const serviceDocs = { schemas: {}, refs: {} };
139-
const sanitizeSchemaFn = sanitizeSchema || exports.defaultSanitizeSchema;
170+
const transformSchemaFn = transformSchema || exports.defaultTransformSchema;
140171

141172
let unspecificSchemas;
142173
const prefix = determineSchemaPrefix(schemas);
@@ -159,7 +190,7 @@ exports.createSwaggerServiceOptions = function createSwaggerServiceOptions ({ sc
159190
const listSchemaName = `${baseSchemeName}List`;
160191

161192
serviceDocs.schemas = {
162-
[baseSchemeName]: sanitizeSchemaFn(resultSchema),
193+
[baseSchemeName]: transformSchemaFn(resultSchema),
163194
[listSchemaName]: {
164195
type: 'array',
165196
items: { $ref: `#/components/schemas/${baseSchemeName}` }
@@ -170,7 +201,7 @@ exports.createSwaggerServiceOptions = function createSwaggerServiceOptions ({ sc
170201

171202
if (dataSchema) {
172203
const dataSchemeName = dataSchema.$id || `${baseSchemeName}Data`;
173-
serviceDocs.schemas[dataSchemeName] = sanitizeSchemaFn(dataSchema);
204+
serviceDocs.schemas[dataSchemeName] = transformSchemaFn(dataSchema);
174205
serviceDocs.refs.createRequest = dataSchemeName;
175206
serviceDocs.refs.updateRequest = dataSchemeName;
176207
}
@@ -181,13 +212,13 @@ exports.createSwaggerServiceOptions = function createSwaggerServiceOptions ({ sc
181212
properties: omit(baseQuerySchema.properties, ['$limit', '$skip'])
182213
};
183214
const querySchemaName = querySchema.$id || `${baseSchemeName}Query`;
184-
serviceDocs.schemas[querySchemaName] = sanitizeSchemaFn(querySchema);
215+
serviceDocs.schemas[querySchemaName] = transformSchemaFn(querySchema);
185216
serviceDocs.refs.queryParameters = querySchemaName;
186217
}
187218

188219
if (patchSchema) {
189220
const patchSchemaName = patchSchema.$id || `${baseSchemeName}PatchData`;
190-
serviceDocs.schemas[patchSchemaName] = sanitizeSchemaFn(patchSchema);
221+
serviceDocs.schemas[patchSchemaName] = transformSchemaFn(patchSchema);
191222
serviceDocs.refs.patchRequest = patchSchemaName;
192223
}
193224

@@ -203,7 +234,7 @@ exports.createSwaggerServiceOptions = function createSwaggerServiceOptions ({ sc
203234
Object.entries(unspecificSchemas).forEach(([key, schema]) => {
204235
const schemaName = schema.$id;
205236
if (schemaName) {
206-
serviceDocs.schemas[schemaName] = sanitizeSchemaFn(schema);
237+
serviceDocs.schemas[schemaName] = transformSchemaFn(schema);
207238
if (allowedDefaultRefs.includes(key)) {
208239
serviceDocs.refs[key] = schemaName;
209240
}

0 commit comments

Comments
 (0)