Skip to content

Commit 5aa4c24

Browse files
committed
fix: adjustments for pre.34+: use dove generated patchSchema instead of generating one
1 parent da062ae commit 5aa4c24

File tree

6 files changed

+599
-371
lines changed

6 files changed

+599
-371
lines changed

docs/examples/generated_service_v5.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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-11", "lineNumbers": true}
5+
```js {"highlight": "9-12", "lineNumbers": true}
66
import { createSwaggerServiceOptions } from 'feathers-swagger';
77
import {
88
userDataValidator,
@@ -14,6 +14,7 @@
1414
userDataSchema,
1515
userQuerySchema,
1616
userSchema,
17+
userPatchSchema,
1718
} from './users.schema';
1819
```
1920
adjust the options when the service is generated
@@ -24,7 +25,7 @@ adjust the options when the service is generated
2425
// You can add additional custom events to be sent to clients here
2526
events: [],
2627
docs: createSwaggerServiceOptions({
27-
schemas: { userDataSchema, userQuerySchema, userSchema },
28+
schemas: { userDataSchema, userQuerySchema, userSchema, userPatchSchema },
2829
docs: {
2930
// any options for service.docs can be added here
3031
securities: ['find', 'get', 'update', 'patch', 'remove'],

lib/custom-methods.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ if (checkImport('@feathersjs/express/rest', 'HTTP_METHOD')) {
5252

5353
getCustomMethods = (app, service, defaultMethods, basePath, doc, serviceOptions) => {
5454
const serviceCustomMethods = [];
55-
(serviceOptions || getServiceOptions(service)).methods.forEach((method) => {
55+
const localServiceOptions = (serviceOptions || getServiceOptions(service));
56+
if (!localServiceOptions || !localServiceOptions.methods) {
57+
return serviceCustomMethods;
58+
}
59+
localServiceOptions.methods.forEach((method) => {
5660
if (defaultServiceMethods.includes(method)) {
5761
return;
5862
}

lib/utils.js

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,55 +113,83 @@ exports.defaultSanitizeSchema = function defaultSanitizeSchema (schema) {
113113
]);
114114
};
115115

116+
function determineSchemaPrefix (schemas) {
117+
const dataSchemaKey = Object.keys(schemas).find(value => value.endsWith('DataSchema'));
118+
if (dataSchemaKey) {
119+
return dataSchemaKey.replace(/DataSchema$/, '');
120+
}
121+
const querySchemaKey = Object.keys(schemas).find(value => value.endsWith('QuerySchema'));
122+
if (querySchemaKey) {
123+
return querySchemaKey.replace(/QuerySchema$/, '');
124+
}
125+
const patchSchemaKey = Object.keys(schemas).find(value => value.endsWith('PatchSchema'));
126+
if (patchSchemaKey) {
127+
return patchSchemaKey.replace(/PatchSchema$/, '');
128+
}
129+
const schemaKey = Object.keys(schemas).find(value => value.endsWith('Schema'));
130+
if (schemaKey) {
131+
return schemaKey.replace(/Schema$/, '');
132+
}
133+
134+
return undefined;
135+
}
136+
116137
exports.createSwaggerServiceOptions = function createSwaggerServiceOptions ({ schemas, docs, sanitizeSchema }) {
117138
const serviceDocs = { schemas: {}, refs: {} };
118139
const sanitizeSchemaFn = sanitizeSchema || exports.defaultSanitizeSchema;
119140

120141
let unspecificSchemas;
121-
const dataSchemaKey = Object.keys(schemas).find(value => value.endsWith('DataSchema'));
122-
if (dataSchemaKey) {
123-
const prefix = dataSchemaKey.replace(/DataSchema$/, '');
124-
const querySchemaKey = `${prefix}QuerySchema`;
142+
const prefix = determineSchemaPrefix(schemas);
143+
if (prefix) {
125144
const resultSchemaKey = `${prefix}Schema`;
126-
if (schemas[dataSchemaKey] && schemas[querySchemaKey] && schemas[resultSchemaKey]) {
145+
const dataSchemaKey = `${prefix}DataSchema`;
146+
const querySchemaKey = `${prefix}QuerySchema`;
147+
const patchSchemaKey = `${prefix}PatchSchema`;
148+
if (schemas[resultSchemaKey]) {
127149
const {
128150
[dataSchemaKey]: dataSchema,
129151
[querySchemaKey]: baseQuerySchema,
130152
[resultSchemaKey]: resultSchema,
153+
[patchSchemaKey]: patchSchema,
131154
...otherSchemas
132155
} = schemas;
133156
unspecificSchemas = otherSchemas;
134157

135-
const querySchema = {
136-
...baseQuerySchema,
137-
properties: omit(baseQuerySchema.properties, ['$limit', '$skip'])
138-
};
139-
140158
const baseSchemeName = resultSchema.$id || prefix;
141-
const dataSchemeName = dataSchema.$id || `${baseSchemeName}Data`;
142-
const querySchemaName = querySchema.$id || `${baseSchemeName}Query`;
143159
const listSchemaName = `${baseSchemeName}List`;
144160

145-
const patchSchemaName = `${baseSchemeName}PatchData`;
146-
const patchSchema = omit(dataSchema, ['required', '$id']);
147-
148161
serviceDocs.schemas = {
149162
[baseSchemeName]: sanitizeSchemaFn(resultSchema),
150-
[dataSchemeName]: sanitizeSchemaFn(dataSchema),
151-
[querySchemaName]: sanitizeSchemaFn(querySchema),
152-
[patchSchemaName]: sanitizeSchemaFn(patchSchema),
153163
[listSchemaName]: {
154164
type: 'array',
155165
items: { $ref: `#/components/schemas/${baseSchemeName}` }
156166
}
157167
};
158168

159-
serviceDocs.refs = {
160-
createRequest: dataSchemeName,
161-
updateRequest: dataSchemeName,
162-
patchRequest: patchSchemaName,
163-
queryParameters: querySchemaName
164-
};
169+
serviceDocs.refs = {};
170+
171+
if (dataSchema) {
172+
const dataSchemeName = dataSchema.$id || `${baseSchemeName}Data`;
173+
serviceDocs.schemas[dataSchemeName] = sanitizeSchemaFn(dataSchema);
174+
serviceDocs.refs.createRequest = dataSchemeName;
175+
serviceDocs.refs.updateRequest = dataSchemeName;
176+
}
177+
178+
if (baseQuerySchema) {
179+
const querySchema = {
180+
...baseQuerySchema,
181+
properties: omit(baseQuerySchema.properties, ['$limit', '$skip'])
182+
};
183+
const querySchemaName = querySchema.$id || `${baseSchemeName}Query`;
184+
serviceDocs.schemas[querySchemaName] = sanitizeSchemaFn(querySchema);
185+
serviceDocs.refs.queryParameters = querySchemaName;
186+
}
187+
188+
if (patchSchema) {
189+
const patchSchemaName = patchSchema.$id || `${baseSchemeName}PatchData`;
190+
serviceDocs.schemas[patchSchemaName] = sanitizeSchemaFn(patchSchema);
191+
serviceDocs.refs.patchRequest = patchSchemaName;
192+
}
165193

166194
if (!docs || !docs.model) {
167195
serviceDocs.model = baseSchemeName;

0 commit comments

Comments
 (0)