diff --git a/src/meta-schema.test.ts b/src/meta-schema.test.ts index fa1a975..6bd8df2 100644 --- a/src/meta-schema.test.ts +++ b/src/meta-schema.test.ts @@ -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; @@ -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', () => { diff --git a/src/meta-schema.ts b/src/meta-schema.ts index c50cac4..f9712f4 100644 --- a/src/meta-schema.ts +++ b/src/meta-schema.ts @@ -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; };