Skip to content

Commit

Permalink
feat: support for x-additionalPropertiesName (#132)
Browse files Browse the repository at this point in the history
Fixes #131
  • Loading branch information
burnpiro authored Feb 8, 2022
1 parent 8c8a820 commit 986093e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Tool for generation samples based on OpenAPI payload/response schema

- Deterministic (given a particular input, will always produce the same output)
- Supports compound keywords: `allOf`, `oneOf`, `anyOf`, `if/then/else`
- Supports `additionalProperties`
- Supports `additionalProperties` with [`x-additionalPropertiesName`](https://github.com/Redocly/redoc/blob/master/docs/redoc-vendor-extensions.md#x-additionalpropertiesname)
- Uses `const`, `examples`, `enum` and `default` where possible - in this order
- Good array support: supports `contains`, `minItems`, `maxItems`, and tuples (`items` as an array)
- Supports `minLength`, `maxLength`, `min`, `max`, `exclusiveMinimum`, `exclusiveMaximum`
Expand Down
5 changes: 3 additions & 2 deletions src/samplers/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export function sampleObject(schema, options = {}, spec, context) {
}

if (schema && typeof schema.additionalProperties === 'object') {
res.property1 = traverse(schema.additionalProperties, options, spec, {depth: depth + 1 }).value;
res.property2 = traverse(schema.additionalProperties, options, spec, {depth: depth + 1 }).value;
const propertyName = schema.additionalProperties['x-additionalPropertiesName'] || 'property';
res[`${String(propertyName)}1`] = traverse(schema.additionalProperties, options, spec, {depth: depth + 1 }).value;
res[`${String(propertyName)}2`] = traverse(schema.additionalProperties, options, spec, {depth: depth + 1 }).value;
}
return res;
}
8 changes: 8 additions & 0 deletions test/unit/object.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ describe('sampleObject', () => {
});
});

it('should use custom property name when x-additionalPropertiesName is defined', () => {
res = sampleObject({additionalProperties: {type: 'string', 'x-additionalPropertiesName': 'attribute-name'}});
expect(res).to.deep.equal({
'attribute-name1': 'string',
'attribute-name2': 'string'
});
});

it('should skip non-required properties if skipNonRequired=true', () => {
res = sampleObject({
properties: {
Expand Down

0 comments on commit 986093e

Please sign in to comment.