-
-
Notifications
You must be signed in to change notification settings - Fork 209
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
serDes is not working with OpenAPI parameters #601
Comments
@pilerou would you be interested in investigating this feature. it's essentially an extension of the |
Hi @agavazov and @cdimascio |
Hi @agavazov paths:
/users/{id}:
get:
parameters:
- name: id
in: path
required: true
schema:
$ref: "#/components/schemas/ObjectId"
....
components:
schemas:
ObjectId:
type: string
format: mongo-objectid
pattern: '^[0-9a-fA-F]{24}$' But If you replace paths:
/users/{id}:
get:
parameters:
- name: id
in: path
required: true
schema:
type: string
format: mongo-objectid
pattern: '^[0-9a-fA-F]{24}$'
....
components:
schemas:
ObjectId:
type: string
format: mongo-objectid
pattern: '^[0-9a-fA-F]{24}$' A quick fix for you could be to create a declaration in |
Hi @cdimascio
but not when the schema is declared directly in route I think the solution would be to adapt I'm not very comfortable to make changes in Do you see how it should be changed ? |
@pilerou I tried your workaround, and effectively, when declaring the type in the |
@jazzzz It's strange. The test validates this use case. Here is an example of serdes configuration for date-time : {
format : 'date-time',
serialize: (d: Date) => {
return d && d.toISOString();
},
deserialize: (s: string) => {
return new Date(s);
}
} How is your serdes configuration ? Could you paste your configuration here and I test ? |
I found the issue: my parameters are declared under the route, not under the method, like this: paths:
/users/{id}:
parameters:
- name: id
in: path
required: true
schema:
type: string
format: mongo-objectid
pattern: '^[0-9a-fA-F]{24}$'
get: The issue also happens for basic types, so it's not related to |
I'm also seeing an issue with a deserialization of a query param. The function for the custom format never gets called. |
I think i found the reason.
Instead of getting a BadRequest Error (400), we get an Internal Server Error (500). Here is the AJV generated code (unflatten). As we can see, even if pattern check fails, it also do format check by deserializing ObjectId... and It fails. if (typeof data2 === "string") {
if (!pattern0.test(data2)) {
var err = {
keyword: 'pattern',
dataPath: (dataPath || '') + '.params.id',
schemaPath: '#/properties/params/properties/id/pattern',
params: {pattern: '^[0-9a-fA-F]{24}$'},
message: 'should match pattern "^[0-9a-fA-F]{24}$"'
};
if (vErrors === null) vErrors = [err]; else vErrors.push(err);
errors++;
}
}
customRule2.errors = null;
var errs__2 = errors;
var valid2;
customRule2.errors = null;
valid2 = customRule2.call(self, data2, (dataPath || '') + '.params.id', data1, 'id', rootData);
if (data1) data2 = data1['id'];
if (!valid2) {
if (Array.isArray(customRule2.errors)) {
if (vErrors === null) vErrors = customRule2.errors; else vErrors = vErrors.concat(customRule2.errors);
errors = vErrors.length;
for (var i2 = errs__2; i2 < errors; i2++) {
var ruleErr2 = vErrors[i2];
if (ruleErr2.dataPath === undefined) ruleErr2.dataPath = (dataPath || '') + '.params.id';
ruleErr2.schemaPath = "#/properties/params/properties/id/x-eov-serdes";
}
} else {
var err = {
keyword: 'x-eov-serdes',
dataPath: (dataPath || '') + '.params.id',
schemaPath: '#/properties/params/properties/id/x-eov-serdes',
params: {keyword: 'x-eov-serdes'},
message: 'should pass "x-eov-serdes" keyword validation'
};
if (vErrors === null) vErrors = [err]; else vErrors.push(err);
errors++;
}
}
var valid2 = errors === errs_2; The best thing, I think is to deal with serialize and deserialize error with a try catch. if (request) {
if (options.serDesMap) {
ajv.addKeyword('x-eov-serdes', {
modifying: true,
compile: (sch) => {
if (sch) {
return function validate(data, path, obj, propName) {
if (typeof data === 'object') return true;
if(!!sch.deserialize) {
try {
obj[propName] = sch.deserialize(data);
}
catch(e) {
return false;
}
}
return true;
};
}
return () => true;
},
});
} If you are OK, I can add make it work in my fork and make a feature request. The unique problem is the answer. We get two errors. We could only have one. {
"name": "Bad Request",
"message": "request.params.id should match pattern \"^[0-9a-fA-F]{24}$\", request.params.id should pass \"x-eov-serdes\" keyword validation",
"status": 400,
"errors": [
{
"path": ".params.id",
"message": "should match pattern \"^[0-9a-fA-F]{24}$\"",
"errorCode": "pattern.openapi.validation"
},
{
"path": ".params.id",
"message": "should pass \"x-eov-serdes\" keyword validation",
"errorCode": "x-eov-serdes.openapi.validation"
}
]
} |
… Server Error and return BadRequest errors cdimascio#601
Describe the bug
When there is described parameter with format in parameters section they are not handled by serDes.
To Reproduce
Add
format: customOne
inrequestBody
and inparameters
and define handler inserDes
, only therequestBody
will be handled.Actual behavior
Only the
requestBody
formats are handled byserDes
definitions.Expected behavior
All inputs must be handled by
serDes
definitions.Examples and context
index.js
openapi.yaml
The text was updated successfully, but these errors were encountered: