feathersjs-ecosystem / feathers-swagger

Add documentation to your FeatherJS services and feed them to Swagger UI.
MIT License
225 stars 63 forks source link

Question: How to override create operation request body #212

Closed alexjs-dev closed 4 years ago

alexjs-dev commented 4 years ago

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 4 years ago

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

Mairu commented 4 years ago

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.