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

Multiple content types in requestBody problem #3234

Closed
amrsalama opened this issue Jun 25, 2019 · 1 comment · Fixed by #3239
Closed

Multiple content types in requestBody problem #3234

amrsalama opened this issue Jun 25, 2019 · 1 comment · Fixed by #3239
Assignees
Labels
bug REST Issues related to @loopback/rest package and REST transport in general Validation

Comments

@amrsalama
Copy link
Contributor

Steps to reproduce

With a simple model Product

@model({settings: {}})
export class Product extends Entity {
  @property({
    type: 'number',
    id: true,
    required: true,
  })
  id: number;

  @property({
    type: 'string',
    required: true,
  })
  name: string;

  constructor(data?: Partial<Product>) {
    super(data);
  }
}

and controller operation with @requestBody has application/x-www-form-urlencoded and application/json as content types

@post('/products')
ping(
  @requestBody({
    content: {
      'application/x-www-form-urlencoded': {},
      'application/json': {},
    },
  })
  product: Product,
): object {
  return {product};
}

Send request with Content-Type: application/json

curl -X POST \
  http://localhost:3000/products \
  -H 'Content-Type: application/json' \
  -d '{"id": 10, "name": "Simple product"}'

And then send request with Content-Type: application/x-www-form-urlencoded

curl -X POST \
  http://localhost:3000/products \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'id=15&name=Simple%20product'

The bug appears (UnprocessableEntityError).

Stop the app and start it again and reverse requests, First send request with Content-Type: application/x-www-form-urlencoded and then send request with Content-Type: application/json, The bug disappears.

Current Behavior

When we have an operation with multiple content types (application/x-www-form-urlencoded and application/json)
If we send request with Content-Type: application/json and then send request with Content-Type: application/x-www-form-urlencoded we get UnprocessableEntityError

{
    "error": {
        "statusCode": 422,
        "name": "UnprocessableEntityError",
        "message": "The request body is invalid. See error object `details` property for more info.",
        "code": "VALIDATION_FAILED",
        "details": [
            {
                "path": ".id",
                "code": "type",
                "message": "should be number",
                "info": {
                    "type": "number"
                }
            }
        ]
    }
}

But if we reverse requests we don't get this problem.

Additional information

linux x64 10.16.0
[email protected] /home/amr/Desktop/loopback-app
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── @loopback/[email protected]
├── [email protected]

@amrsalama amrsalama added the bug label Jun 25, 2019
@raymondfeng raymondfeng self-assigned this Jun 25, 2019
@raymondfeng
Copy link
Contributor

I can reproduce the problem. The root cause is that we cache AJV validators but the options is not taken into consideration. The json body does not require coercion while urlencoded body requires. As a result, the cached AJV validator does not support coercion if it's used first.

I'll look into a fix tomorrow.

@bajtos bajtos added REST Issues related to @loopback/rest package and REST transport in general Validation labels Jun 25, 2019
@dhmlau dhmlau added the 2019Q3 label Jun 25, 2019
@nabdelgadir nabdelgadir added this to the July 2019 milestone milestone Jun 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug REST Issues related to @loopback/rest package and REST transport in general Validation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants