Skip to content

Commit

Permalink
fix: handle type: "object" in schema composition (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
vwong committed Oct 9, 2023
1 parent 5d84fcc commit f9219ad
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ValidateOptions } from '../types';
import { isTypesOfJson } from './content-negotiation';
import { validateJson } from './validate-json';

const transformSchema = (
export const transformSchema = (
schema: ParsedSpecJsonSchema,
opts: Pick<ValidateOptions, 'additionalPropertiesInResponse' | 'requiredPropertiesInResponse'>
): ParsedSpecJsonSchema => {
Expand All @@ -18,7 +18,11 @@ const transformSchema = (
if (!opts.additionalPropertiesInResponse) {
traverseJsonSchema(modifiedSchema, (mutableSchema) => {
if (
(typeof mutableSchema.additionalProperties === 'undefined' || mutableSchema.additionalProperties === true) &&
(typeof mutableSchema.additionalProperties === 'undefined' ||
mutableSchema.additionalProperties === true) &&
!mutableSchema.oneOf &&
!mutableSchema.allOf &&
!mutableSchema.anyOf &&
mutableSchema.type &&
mutableSchema.type === 'object'
) {
Expand Down
48 changes: 48 additions & 0 deletions test/unit/schema-transformation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { transformSchema } from '../../lib/swagger-mock-validator/validate-spec-and-mock/validate-parsed-mock-response-body';

// defaults
const options = {
additionalPropertiesInResponse: false,
requiredPropertiesInResponse: false,
};

const transformedAdditionalProps = (schema) => transformSchema(schema, options).additionalProperties;
const transformedRequired = (schema) => transformSchema(schema, options).required;

describe('response transformation', () => {
// a provider must provide a superset of what the consumer asks for
// additionalProperties expected in pact response are disallowed
describe('additionalProperties', () => {
it('is prevented in objects', () => {
expect(transformedAdditionalProps({ type: 'object' })).toBeFalse();
});

it('is forced to be false', () => {
expect(transformedAdditionalProps({ type: 'object', additionalProperties: true })).toBeFalse();
});

it('allows schema composition', () => {
expect(transformedAdditionalProps({ type: 'object', oneOf: [] })).toBeUndefined();
expect(transformedAdditionalProps({ type: 'object', allOf: [] })).toBeUndefined();
expect(transformedAdditionalProps({ type: 'object', anyOf: [] })).toBeUndefined();
});
});

// a consumer may only use a subset of the provider *response*
// any field marked as required in OAS, should be considered optional for pact testing
describe('required properties', () => {
it('makes properties optional', () => {
expect(
transformedRequired({
type: 'object',
required: ['foo'],
properties: {
foo: {
type: 'string',
},
},
})
).toBeUndefined();
});
});
});

0 comments on commit f9219ad

Please sign in to comment.