Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 87112f5

Browse files
committed
feat(oas3): support Schema Object const
1 parent b51b62a commit 87112f5

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

packages/openapi3-parser/lib/parser/oas/parseSchemaObject.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,12 @@ function parseSchema(context) {
258258
return element;
259259
});
260260

261+
const parseConst = (value) => {
262+
const element = value.clone();
263+
element.attributes.set('typeAttributes', ['fixed']);
264+
return element;
265+
};
266+
261267
const parseMember = R.cond([
262268
[hasKey('type'), R.compose(parseType(context), getValue)],
263269
[hasKey('enum'), R.compose(parseEnum(context, name), getValue)],
@@ -271,6 +277,11 @@ function parseSchema(context) {
271277
[hasKey('example'), e => e.clone()],
272278
[hasKey('oneOf'), R.compose(parseOneOf, getValue)],
273279

280+
[
281+
R.both(hasKey('const'), R.always(context.isOpenAPIVersionMoreThanOrEqual(3, 1))),
282+
R.compose(parseConst, getValue),
283+
],
284+
274285
[isUnsupportedKey, createUnsupportedMemberWarning(namespace, name)],
275286
[
276287
R.both(
@@ -292,11 +303,14 @@ function parseSchema(context) {
292303
let element;
293304

294305
const oneOf = schema.get('oneOf');
306+
const constValue = schema.get('const');
295307
const enumerations = schema.get('enum');
296308
const type = schema.getValue('type') || [];
297309

298310
if (oneOf) {
299311
element = oneOf;
312+
} else if (constValue) {
313+
element = constValue;
300314
} else if (enumerations) {
301315
element = enumerations;
302316
} else if (type.length > 1) {

packages/openapi3-parser/test/unit/parser/oas/parseSchemaObject-test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,36 @@ describe('Schema Object', () => {
310310
});
311311
});
312312

313+
describe('#const', () => {
314+
it('warns when used on OpenAPI 3.0', () => {
315+
const schema = new namespace.elements.Object({
316+
const: { message: 'Hello' },
317+
});
318+
const parseResult = parse(context, schema);
319+
320+
expect(parseResult).to.contain.warning(
321+
"'Schema Object' contains invalid key 'const'"
322+
);
323+
});
324+
325+
it('returns a fixed element', () => {
326+
context.openapiVersion = { major: 3, minor: 1 };
327+
const schema = new namespace.elements.Object({
328+
const: { message: 'Hello' },
329+
});
330+
const parseResult = parse(context, schema);
331+
332+
expect(parseResult.length).to.equal(1);
333+
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.DataStructure);
334+
expect(parseResult).to.not.contain.annotations;
335+
336+
const element = parseResult.get(0).content;
337+
expect(element).to.be.instanceof(namespace.elements.Object);
338+
expect(element.toValue()).to.deep.equal({ message: 'Hello' });
339+
expect(element.attributes.getValue('typeAttributes')).to.deep.equal(['fixed']);
340+
});
341+
});
342+
313343
describe('object type', () => {
314344
describe('#required', () => {
315345
it('warns when required is not an array', () => {

0 commit comments

Comments
 (0)