Skip to content

Commit

Permalink
Merge pull request #39 from lifeomic/avoid-assumptions-for-introspection
Browse files Browse the repository at this point in the history
fix: avoid applying assumptions for introspected schemas
  • Loading branch information
swain authored Jun 22, 2022
2 parents 340cdf3 + 10c53f8 commit cdf0f7d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
51 changes: 51 additions & 0 deletions src/meta-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {
} from './meta-schema';
import { IntrospectionResponse } from './types';

beforeEach(() => {
jest.spyOn(console, 'log').mockReturnValue(void 0);
});

describe('assumptions', () => {
const FIXTURES: {
name: string;
Expand Down Expand Up @@ -191,6 +195,53 @@ describe('loadSchemaFromFile', () => {
Endpoints: {},
});
});

test('does not apply assumptions to introspection responses', () => {
const introspectionResponse: IntrospectionResponse = {
serviceVersion: 'mock-service-version',
schema: {
Endpoints: {
'GET /posts': {
Name: 'getPosts',
Response: {
type: 'object',
properties: {
something: { type: 'string' },
},
},
},
},
},
};

const filename = tmpNameSync();
writeFileSync(filename, dump(introspectionResponse), {
encoding: 'utf-8',
});

const result = loadSchemaFromFile(filename, {
objectPropertiesRequiredByDefault: true,
noAdditionalPropertiesOnObjects: true,
});

expect(result).toStrictEqual({
Endpoints: {
'GET /posts': {
Name: 'getPosts',
Response: {
type: 'object',
properties: {
something: { type: 'string' },
},
},
},
},
});

expect(console.log).toHaveBeenCalledWith(
'Detected one-schema introspection response. Skipping applying schema assumptions.',
);
});
});

describe('validateSchema', () => {
Expand Down
17 changes: 14 additions & 3 deletions src/meta-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,23 @@ export const loadSchemaFromFile = (
): OneSchemaDefinition => {
let spec: any = load(readFileSync(filename, { encoding: 'utf-8' }));

// Check if this in an introspection result. If so, grab the schema.
if (typeof spec === 'object' && 'schema' in spec) {
let applyAssumptions = true;

// Check if this in an introspection result. If so, grab the schema,
// and skip applying assumptions. Schemas served directly from services
// should always be correct + complete.
const isIntrospectionResult = typeof spec === 'object' && 'schema' in spec;
if (isIntrospectionResult) {
spec = spec.schema;
applyAssumptions = false;
console.log(
'Detected one-schema introspection response. Skipping applying schema assumptions.',
);
}

validateSchema(spec as OneSchemaDefinition);

return withAssumptions(spec as OneSchemaDefinition, assumptions);
return applyAssumptions
? withAssumptions(spec as OneSchemaDefinition, assumptions)
: spec;
};

0 comments on commit cdf0f7d

Please sign in to comment.