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

Prefer variable key from examples when possible #614

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion packages/openapi2-parser/lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ class DataStructureGenerator {
element.attributes.set('typeAttributes', typeAttributes);

if (schema.additionalProperties !== false) {
const member = this.generateMember('', schema.additionalProperties);
let key;

if (schema.example && typeof schema.example === 'object') {
key = Object.keys(schema.example).find(key => element.getMember(key) === undefined);
}

const member = this.generateMember(key || '', schema.additionalProperties);
member.attributes.set('variable', true);
element.content.push(member);
}
Expand Down
86 changes: 86 additions & 0 deletions packages/openapi2-parser/test/schema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,92 @@ describe('JSON Schema to Data Structure', () => {
expect(mode.attributes.getValue('typeAttributes')).to.deep.equal(['required']);
expect(mode.value).to.be.instanceof(StringElement);
});

describe('infering from example', () => {
it('produces object with additionalProperties containing example', () => {
const schema = {
type: 'object',
additionalProperties: {
type: 'string',
},
example: {
'application/json': 'https://tools.ietf.org/html/rfc7159',
},
};

const dataStructure = schemaToDataStructure(schema);

expect(dataStructure.element).to.equal('dataStructure');
expect(dataStructure.content).to.be.instanceof(ObjectElement);
expect(dataStructure.content.attributes.getValue('typeAttributes')).to.deep.equal(['fixedType']);

expect(dataStructure.content.length).to.equal(1);

const variable = dataStructure.content.getMember('application/json');
expect(variable).to.be.instanceof(MemberElement);
expect(variable.attributes.getValue('variable')).to.be.true;
expect(variable.value).to.be.instanceof(StringElement);
expect(variable.value.toValue()).to.be.undefined;
Copy link
Member Author

Choose a reason for hiding this comment

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

I was trying to see if we could "move" the example value here too, but it does get tricky quick due to the complexities when the schema for the additional properties could contain various constraints (fixed, fixedType, be a container, etc) which change what the "value" means.

I'll perhaps follow up with that when I get a chance.

});

it('produces object with additionalProperties containing example excluding required', () => {
const schema = {
type: 'object',
additionalProperties: {
type: 'string',
},
required: ['application/json'],
example: {
'application/json': 'https://tools.ietf.org/html/rfc7159',
},
};

const dataStructure = schemaToDataStructure(schema);

expect(dataStructure.element).to.equal('dataStructure');
expect(dataStructure.content).to.be.instanceof(ObjectElement);
expect(dataStructure.content.attributes.getValue('typeAttributes')).to.deep.equal(['fixedType']);

expect(dataStructure.content.length).to.equal(2);

const variable = dataStructure.content.getMember('');
expect(variable).to.be.instanceof(MemberElement);
expect(variable.attributes.getValue('variable')).to.be.true;
expect(variable.value).to.be.instanceof(StringElement);
expect(variable.value.toValue()).to.be.undefined;
});

it('produces object with additionalProperties containing example excluding defined properties', () => {
const schema = {
type: 'object',
properties: {
'application/json': {
type: 'boolean',
},
},
additionalProperties: {
type: 'string',
},
example: {
'application/json': 'https://tools.ietf.org/html/rfc7159',
},
};

const dataStructure = schemaToDataStructure(schema);

expect(dataStructure.element).to.equal('dataStructure');
expect(dataStructure.content).to.be.instanceof(ObjectElement);
expect(dataStructure.content.attributes.getValue('typeAttributes')).to.deep.equal(['fixedType']);

expect(dataStructure.content.length).to.equal(2);

const variable = dataStructure.content.getMember('');
expect(variable).to.be.instanceof(MemberElement);
expect(variable.attributes.getValue('variable')).to.be.true;
expect(variable.value).to.be.instanceof(StringElement);
expect(variable.value.toValue()).to.be.undefined;
});
});
});

context('array schema', () => {
Expand Down