Question: How to override create operation request body
Closed this issue · 2 comments
Hello,
I have successfully imported a mongoose model into feathers-swagger docs, and am getting correct data in all of my requests, however the data for POST request must be changed, because some fields are read-only.
Currently there is no way to do it... My tries:
const readOnlyFields = ['isVerified', 'onlineAt', 'role']; // Need to remove these fields from POST request
usersService.docs = {
description: 'Registration & User service',
operations: {
create: {
description: 'Registration service. Combines user and user profile services',
definitions: {
users: {
properties: {
...omit(swaggerSchema.properties, readOnlyFields), // LODASH OMIT
profile: {
type: 'object',
properties: {
...omit(swaggerSchemaProfile.properties, readOnlyFields)
}
}
},
},
}
}
},
definitions: {
users: {
properties: {
...swaggerSchema.properties,
profile: {
type: 'object',
properties: {
...swaggerSchemaProfile.properties
}
}
},
},
'users_list': {
type: 'array',
items: { $ref: '#/definitions/users' }
}
},
};
swaggerSchemaProfile.properties looks like:
firstName: { type: 'string' },
isVerified: { type: 'boolean' },
_id: { type: 'string' },
updatedAt: { type: 'string', format: 'date-time' },
createdAt: { type: 'string', format: 'date-time' } }
Hi,
this is not a bug. It is possible in multiple ways:
- overwrite the response directly in docs.operation.create -> path to overwrite would be 'responses.201.schema.properties' if you would want to overwrite the properties directly -> see https://github.com/feathersjs-ecosystem/feathers-swagger/blob/master/example/swagger-v2/definitions.js#L46 as example how to overwrite nested paths (there only the descriptions is changed)
- create another definition for the createResponse and set it in the refs options as createResponse -> see https://github.com/feathersjs-ecosystem/feathers-swagger/blob/master/example/swagger-v2/customMethods.js for exmaple to use additional definitions and referencing them in refs, although there it is only used for custom methods, I think you should get how to use it
feathers-swagger is just a helper library to create a swagger definition. And there you have to define different definitions, if that is necessary.
feathers-swagger has abilities to automatic generate stuff either by using operationsGenerators (example) or by using getOperationRefs and schemasGenerator (example) together.
Still it will be more straightforward to manually overwrite where necessary.