Skip to content

Commit

Permalink
Fixes on SerDes (#682)
Browse files Browse the repository at this point in the history
* Try catch serdes serialize and deserialize in order to avoid Internal Server Error and return BadRequest errors #601

* Fix incorrect serDes example #569
  • Loading branch information
pilerou authored Dec 21, 2021
1 parent 3f0de5d commit 56f778b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ To create custom serializers and/or deserializers, define:
e.g.

```javascript
serDes: [{
serDes: [
// installs dateTime serializer and deserializer
OpenApiValidator.serdes.dateTime,
// installs date serializer and deserializer
Expand All @@ -784,8 +784,8 @@ serDes: [{
format: 'mongo-objectid',
deserialize: (s) => new ObjectID(s),
serialize: (o) => o.toString(),
}
}],
},
],
```

The mongo serializers will trigger on the following schema:
Expand Down
14 changes: 12 additions & 2 deletions src/framework/ajv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ function createAjv(
];
return false;
}
obj[propName] = sch.deserialize(data);
try {
obj[propName] = sch.deserialize(data);
}
catch(e) {
return false;
}
}
return true;
};
Expand Down Expand Up @@ -99,7 +104,12 @@ function createAjv(
return function validate(data, path, obj, propName) {
if (typeof data === 'string') return true;
if (!!sch.serialize) {
obj[propName] = sch.serialize(data);
try {
obj[propName] = sch.serialize(data);
}
catch(e) {
return false;
}
}
return true;
};
Expand Down

2 comments on commit 56f778b

@cdimascio
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pilerou i noticed the following test failed
'should throw error 500 on invalid object type instead of Date expected'

  1) serdes serialize response components only
       should throw error 500 on invalid object type instead of Date expected:

      AssertionError: expected '.response.creationDate should pass "x-eov-serdes" keyword validation' to equal 'd.toISOString is not a function'
      + expected - actual

      -.response.creationDate should pass "x-eov-serdes" keyword validation
      +d.toISOString is not a function
      
      at /Users/cdimasci/git/express-openapi-validator/test/serdes.spec.ts:310:35
      at processTicksAndRejections (internal/process/task_queues.js:93:5)

could you take a look to see if the test needs to change or if something broke

@pilerou
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cdimascio I fixed it. I changed the message to a more human readable message.
I add a pull request.

I think it should be even better.
Actually, the string in request shouldn't be deserialized as it doesn't match to the format field.
Have you an idea in order to make change the order of controls in AJV :

  • format control first before serdes on request
  • serdes first before format control on response ?

Please sign in to comment.