Skip to content

Commit af2982c

Browse files
FIx serialization/deserialization in additionalProperties (#822)
1 parent 57cc814 commit af2982c

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

src/middlewares/parsers/schema.preprocessor.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ export class SchemaPreprocessor {
208208
const child = new Node(node, cschema, path);
209209
recurse(node, child, opts);
210210
});
211+
} else if (schema.additionalProperties) {
212+
const child = new Node(node, schema.additionalProperties, [...node.path, 'additionalProperties']);
213+
recurse(node, child, opts);
211214
}
212215
};
213216

test/821.spec.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as express from 'express';
2+
import { Server } from 'http';
3+
import * as request from 'supertest';
4+
import * as OpenApiValidator from '../src';
5+
import { OpenAPIV3 } from '../src/framework/types';
6+
import { startServer } from './common/app.common';
7+
import { deepStrictEqual } from 'assert';
8+
import * as path from 'path';
9+
10+
11+
const apiSpecPath = path.join('test', 'resources', '699.yaml');
12+
13+
const date = new Date()
14+
describe('issue #821 - serialization inside addiotionalProperties', () => {
15+
it('serializa both outer and inner date in addiotionalProperties', async () => {
16+
17+
const app = await createApp(apiSpecPath);
18+
await request(app).get('/test').expect(200,
19+
{
20+
outer_date: date.toISOString(),
21+
other_info: {
22+
something: {
23+
inner_date: date.toISOString()
24+
}
25+
}
26+
}
27+
);
28+
app.server!.close();
29+
});
30+
});
31+
32+
async function createApp(
33+
apiSpec: OpenAPIV3.Document | string,
34+
): Promise<express.Express & { server?: Server }> {
35+
const app = express();
36+
37+
app.use(
38+
OpenApiValidator.middleware({
39+
apiSpec: apiSpecPath,
40+
validateRequests: true,
41+
validateResponses: true,
42+
}),
43+
);
44+
app.get('/test', (req, res) => {
45+
return res.status(200).json({
46+
outer_date: date,
47+
other_info: {
48+
something: {
49+
inner_date: date
50+
}
51+
}
52+
})
53+
54+
})
55+
56+
app.use((err, req, res, next) => {
57+
console.error(err); // dump error to console for debug
58+
res.status(err.status || 500).json({
59+
message: err.message,
60+
errors: err.errors,
61+
});
62+
});
63+
64+
await startServer(app, 3001);
65+
return app;
66+
}
67+
68+

test/resources/821.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
openapi: 3.0.3
2+
info:
3+
version: 1.0.0
4+
title: Test additionalProperties date-time
5+
paths:
6+
/test:
7+
get:
8+
responses:
9+
'200':
10+
description: foo
11+
content:
12+
application/json:
13+
schema:
14+
type: object
15+
properties:
16+
outer_date:
17+
type: string
18+
format: date-time
19+
other_info:
20+
type: object
21+
additionalProperties:
22+
type: object
23+
properties:
24+
inner_date:
25+
type: string
26+
format: date-time

0 commit comments

Comments
 (0)