Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only method is not working as expected #4107

Closed
2 tasks done
AnwerAR opened this issue Jul 3, 2022 · 2 comments · Fixed by #4109
Closed
2 tasks done

only method is not working as expected #4107

AnwerAR opened this issue Jul 3, 2022 · 2 comments · Fixed by #4109
Labels
bug Confirmed bug

Comments

@AnwerAR
Copy link

AnwerAR commented Jul 3, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.2.0

Plugin version

3.1.0

Node.js version

17.8.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.1

Description

Creating multiple schemas out of existing one using the only method is creating conflictions when used with different http methods. For instance, If two schemas for POST and PATCH is extracted from base schema using only keyword then only one of them is getting applied to both routes (first route in the file is taking precedence)

Steps to Reproduce

Below are examples of schemas and routes.

Base schema

const UserSchema = Schema.object()
    .id('http://mydomain.com/user')
    .title('User schema')
    .description('Contains all user fields')
    .prop('id', Schema.integer())
    .prop('username', Schema.string().minLength(4))
    .prop('firstName', Schema.string().minLength(1))
    .prop('lastName', Schema.string().minLength(1))
    .prop('fullName', Schema.string().minLength(1))
    .prop('email', Schema.string())
    .prop('password', Schema.string().minLength(6))
    .prop('bio', Schema.string());

Schema for POST method (All fields are required)

const UserCreateSchema = UserSchema.only([
    'username',
    'firstName',
    'lastName',
    'email',
    'bio',
    'password',
    'password_confirm',
]).required([
    'username',
    'firstName',
    'lastName',
    'email',
    'bio',
    'password',
]);

Schema for PATCH method (Only fields mentioned in patch schema is modifiable and none of them are required.)

const UserPatchSchema = UserSchema.only([
    'firstName',
    'lastName',
    'bio',
]);

1. Routes

app.post('/user', {
    schema: {
        body: UserCreateSchema,
    },
    handler: {CREATE HANDLER},
});

app.patch('/user/:id', {
    schema: {
        body: UserPatchSchema,
    },
    handler: {PATCH HANDLER},
});

As in above snippet, when app.post is declared first then UserCreateSchema is getting applied to both POST & PATCH routes. And if we revert the order of routes like below then UserPatchSchema is getting applied to both routes ignoring rules defined in UserCreateSchema for POST

2. Routes

app.patch('/user/:id', {
    schema: {
        body: UserPatchSchema,
    },
    handler: {PATCH HANDLER},
});

app.post('/user', {
    schema: {
        body: UserCreateSchema,
    },
    handler: {CREATE HANDLER},
});

Expected Behavior

Ideally, schemas created using only keyword should work independently without conflictions.

@Eomm
Copy link
Member

Eomm commented Jul 3, 2022

It is not a fluent schema issue.
To fix it just remove the .id() initialization.

Moving this issue to fastify as it should not start when there are duplicated ids in the same context.

Reproducible snippet:

const fastify = require('./')
const Schema = require('fluent-json-schema')

const UserSchema = Schema.object()
  .id('http://mydomain.com/user')
  .title('User schema')
  .description('Contains all user fields')
  .prop('id', Schema.integer())
  .prop('username', Schema.string().minLength(4))
  .prop('firstName', Schema.string().minLength(1))
  .prop('lastName', Schema.string().minLength(1))
  .prop('fullName', Schema.string().minLength(1))
  .prop('email', Schema.string())
  .prop('password', Schema.string().minLength(6))
  .prop('bio', Schema.string())

const UserCreateSchema = UserSchema.only([
  'username',
  'firstName',
  'lastName',
  'email',
  'bio',
  'password',
  'password_confirm'
])
  .required([
    'username',
    'firstName',
    'lastName',
    'email',
    'bio',
    'password'
  ])

console.log(UserCreateSchema.valueOf())

const UserPatchSchema = UserSchema.only([
  'firstName',
  'lastName',
  'bio'
])

console.log(UserPatchSchema.valueOf())

const app = fastify({ logger: true })
app.patch('/user/:id', {
  schema: {
    body: UserPatchSchema
  },
  handler: () => { return 'ok' }
})

app.post('/user', {
  schema: {
    body: UserCreateSchema
  },
  handler: () => { return 'ok' }
})

app.inject({
  method: 'POST',
  url: '/user',
  body: {}
}, (err, res) => {
  console.log(res.payload)
})

app.inject({
  url: '/user/1',
  method: 'PATCH',
  body: {}
}, (err, res) => {
  console.log(res.payload)
})

@Eomm Eomm transferred this issue from fastify/fluent-json-schema Jul 3, 2022
@Eomm Eomm added the bug Confirmed bug label Jul 3, 2022
@AnwerAR
Copy link
Author

AnwerAR commented Jul 4, 2022

Thank you @Eomm for quick response 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants