Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/openapi3-parser/lib/parser/oas/parseSchemaObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ function parseSchema(context) {
return element;
});

const parseConst = (value) => {
const element = value.clone();
element.attributes.set('typeAttributes', ['fixed']);
return element;
};

const parseMember = R.cond([
[hasKey('type'), R.compose(parseType(context), getValue)],
[hasKey('enum'), R.compose(parseEnum(context, name), getValue)],
Expand All @@ -251,6 +257,11 @@ function parseSchema(context) {
[hasKey('example'), e => e.clone()],
[hasKey('oneOf'), R.compose(parseOneOf, getValue)],

[
R.both(hasKey('const'), R.always(context.isOpenAPIVersionMoreThanOrEqual(3, 1))),
R.compose(parseConst, getValue),
],

[isUnsupportedKey, createUnsupportedMemberWarning(namespace, name)],

// Return a warning for additional properties
Expand All @@ -265,11 +276,14 @@ function parseSchema(context) {
let element;

const oneOf = schema.get('oneOf');
const constValue = schema.get('const');
const enumerations = schema.get('enum');
const type = schema.getValue('type') || [];

if (oneOf) {
element = oneOf;
} else if (constValue) {
element = constValue;
} else if (enumerations) {
element = enumerations;
} else if (type.length > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,36 @@ describe('Schema Object', () => {
});
});

describe('#const', () => {
it('warns when used on OpenAPI 3.0', () => {
const schema = new namespace.elements.Object({
const: { message: 'Hello' },
});
const parseResult = parse(context, schema);

expect(parseResult).to.contain.warning(
"'Schema Object' contains invalid key 'const'"
);
});

it('returns a fixed element', () => {
context.openapiVersion = { major: 3, minor: 1 };
const schema = new namespace.elements.Object({
const: { message: 'Hello' },
});
const parseResult = parse(context, schema);

expect(parseResult.length).to.equal(1);
expect(parseResult.get(0)).to.be.instanceof(namespace.elements.DataStructure);
expect(parseResult).to.not.contain.annotations;

const element = parseResult.get(0).content;
expect(element).to.be.instanceof(namespace.elements.Object);
expect(element.toValue()).to.deep.equal({ message: 'Hello' });
expect(element.attributes.getValue('typeAttributes')).to.deep.equal(['fixed']);
});
});

describe('object type', () => {
describe('#required', () => {
it('warns when required is not an array', () => {
Expand Down