loopbackio / loopback-next

LoopBack makes it easy to build modern API applications that require complex integrations.
https://loopback.io
Other
4.95k stars 1.07k forks source link

openapi.json and validation #6724

Closed mdbetancourt closed 3 years ago

mdbetancourt commented 3 years ago

Hello i used an open api enhancer with the following implementation

modifySpec(spec: OpenApiSpec): OpenApiSpec {
    const schemas = Object.entries(spec.components?.schemas ?? []).map(
      ([key, schema]) => {
        if (!key.includes('.Filter')) return [key, schema]
        return [
          key,
          mergeOpenAPISpec(schema, {
            properties: {
              pivot: {
                title: key.replace('.Filter', '.PivotFilter'),
                type: 'object',
                properties: {},
                additionalProperties: true,
              },
            },
          }),
        ]
      },
    )
    return mergeOpenAPISpec(spec, {
      components: { schemas: Object.fromEntries(schemas) },
    })
  }

the openapi.json is properly updated but validations is still failing image is this behavior expected? or is it a bug? i could fix it with some guidance, because i'm not sure what could be

Steps to reproduce

add the open api enhancer and try to do a request with a filter

Current Behavior

throw an error ignoring the openapi spec

Expected Behavior

works as expected

Related Issues

6436

dhmlau commented 3 years ago

@mdbetancourt, do you have a sample of openapi.json that fails validation? To me, the generate openapi.json should pass validation.

dhmlau commented 3 years ago

@jannyHou, could you please take a look when we get more information? Thanks.

mdbetancourt commented 3 years ago

i enabled DEBUG (loopback:rest:validation,loopback:rest:parser,loopback:rest:parse-param,loopback:rest:find-route) flag for loopback and this is the output loopback:rest:find-route Finding route for GET /users?filter=%7B%0A%20%20%22pivot%22%3A%20%7B%0A%20%20%20%20%22oo%22%3A%205%0A%20%20%7D%0A%7D +0ms loopback:rest:find-route Route found for GET /users?filter=%7B%0A%20%20%22pivot%22%3A%20%7B%0A%20%20%20%20%22oo%22%3A%205%0A%20%20%7D%0A%7D ControllerRoute {} +1ms loopback:rest:parse-param Parsing parameters for get /users +0ms loopback:rest:parser Parsing operation arguments for route get /users => UserController.find +0ms loopback:rest:validation Converted OpenAPI schema to JSON schema:

{
  type: 'object',
  title: 'User.Filter',
  properties: {
    offset: { type: 'integer', minimum: 0 },
    limit: { type: 'integer', minimum: 1 },
    skip: { type: 'integer', minimum: 0 },
    order: {
      oneOf: [
        { type: 'string' },
        { type: 'array', items: { type: 'string' } }
      ]
    },
    where: {
      title: 'User.WhereFilter',
      type: 'object',
      additionalProperties: true
    },
    fields: {
      title: 'User.Fields',
      type: 'object',
      properties: {
        id: { type: 'boolean' },
        createdAt: { type: 'boolean' },
        updatedAt: { type: 'boolean' },
        name: { type: 'boolean' },
        lastName: { type: 'boolean' },
        phone: { type: 'boolean' },
        level: { type: 'boolean' }
      },
      additionalProperties: true
    },
    include: {
      title: 'User.IncludeFilter',
      type: 'array',
      items: {
        title: 'User.IncludeFilter.Items',
        type: 'object',
        properties: {
          relation: { type: 'string' },
          scope: {
            properties: {
              offset: { type: 'integer', minimum: 0 },
              limit: { type: 'integer', minimum: 1 },
              skip: { type: 'integer', minimum: 0 },
              order: {
                oneOf: [
                  { type: 'string' },
                  { type: 'array', items: { type: 'string' } }
                ]
              },
              where: { type: 'object', additionalProperties: true },
              fields: { type: 'object', additionalProperties: true },
              include: {
                type: 'array',
                items: { type: 'object', additionalProperties: true }
              }
            },
            additionalProperties: false,
            title: 'User.ScopeFilter'
          }
        }
      }
    }
  },
  additionalProperties: false,
  'x-typescript-type': '@loopback/repository#Filter<User>'
}

loopback:rest:validation Invalid value: { pivot: { oo: 5 } }. Errors

[
  {
    keyword: 'additionalProperties',
    dataPath: '',
    schemaPath: '#/additionalProperties',
    params: { additionalProperty: 'pivot' },
    message: 'should NOT have additional properties'
  }
]
stale[bot] commented 3 years ago

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

stale[bot] commented 3 years ago

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.