feathersjs-ecosystem/feathers-swagger

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' } }
Mairu commented

Hi,
this is not a bug. It is possible in multiple ways:

Mairu commented

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.