Skip to content

Commit

Permalink
feat(oas): expose hash function (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Apr 7, 2022
1 parent 7a9a87e commit e13dc26
Show file tree
Hide file tree
Showing 23 changed files with 136 additions and 135 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
"types": "./index.d.ts",
"default": "./index.js"
},
"./hash": {
"types": "./hash.d.ts",
"default": "./hash.js"
},
"./oas": {
"types": "./oas/index.d.ts",
"default": "./oas/index.js"
Expand Down Expand Up @@ -65,7 +69,7 @@
},
"dependencies": {
"@stoplight/json": "^3.18.0",
"@stoplight/types": "^13.0.0-beta.5",
"@stoplight/types": "^13.0.0",
"@types/json-schema": "7.0.5",
"@types/swagger-schema-official": "~2.0.21",
"@types/type-is": "^1.6.3",
Expand Down
4 changes: 0 additions & 4 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { hasRef, isLocalRef } from '@stoplight/json';
// @ts-ignore
import * as fnv from 'fnv-plus';

import type {
AvailableContext,
Expand All @@ -11,8 +9,6 @@ import type {
TranslateFunction,
} from './types';

export const DEFAULT_ID_GENERATOR: IdGenerator = input => fnv.fast1a52hex(input);

export function createContext<T extends Record<string, unknown>>(
document: T,
resolveRef: RefResolver<T>,
Expand Down
6 changes: 6 additions & 0 deletions src/hash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @ts-expect-error: no types
import * as fnv from 'fnv-plus';

import type { IdGenerator } from './types';

export const hash: IdGenerator = input => fnv.fast1a52hex(input);
9 changes: 2 additions & 7 deletions src/oas/__tests__/accessors.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { createContext, DEFAULT_ID_GENERATOR } from '../../context';
import { Fragment } from '../../types';
import { createOasParamsIterator } from '../accessors';
import { resolveRef } from '../resolver';
import { createContext } from '../context';
import { OasVersion } from '../types';

const getValidOasParameters = (document: { paths: { '/': { get: Fragment } } }, spec: OasVersion) =>
Array.from(
createOasParamsIterator(spec as any).call(
createContext(document, resolveRef, DEFAULT_ID_GENERATOR),
document.paths['/'],
document.paths['/'].get,
),
createOasParamsIterator(spec as any).call(createContext(document), document.paths['/'], document.paths['/'].get),
);

describe('createOasParamsIterator', () => {
Expand Down
6 changes: 2 additions & 4 deletions src/oas/__tests__/tags.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { createContext, DEFAULT_ID_GENERATOR } from '../../context';
import { resolveRef } from '../resolver';
import { createContext } from '../context';
import { translateToTags as _translateToTags } from '../tags';

const translateToTags = (tags: unknown) =>
_translateToTags.call(createContext({}, resolveRef, DEFAULT_ID_GENERATOR), tags);
const translateToTags = (tags: unknown) => _translateToTags.call(createContext({}), tags);

describe('translateToTags', () => {
describe.each([2, null, {}, '', 0])('when tags property is not an array', tags => {
Expand Down
8 changes: 8 additions & 0 deletions src/oas/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createContext as _createContext } from '../context';
import { hash } from '../hash';
import type { Fragment } from '../types';
import { resolveRef } from './resolver';

export function createContext<T extends Fragment>(document: T) {
return _createContext<T>(document, resolveRef, hash);
}
6 changes: 2 additions & 4 deletions src/oas/transformers/schema/__tests__/schema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { createContext, DEFAULT_ID_GENERATOR } from '../../../../context';
import { resolveRef } from '../../../resolver';
import { createContext } from '../../../context';
import { translateSchemaObject } from '..';
import type { OASSchemaObject } from '../types';

const translate = (schemaObject: OASSchemaObject) =>
translateSchemaObject.call(createContext({}, resolveRef, DEFAULT_ID_GENERATOR), schemaObject);
const translate = (schemaObject: OASSchemaObject) => translateSchemaObject.call(createContext({}), schemaObject);

describe('translateSchemaObject', () => {
it('should translate id', () => {
Expand Down
5 changes: 2 additions & 3 deletions src/oas2/operation.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { DeepPartial, IHttpOperation } from '@stoplight/types';
import type { Spec } from 'swagger-schema-official';

import { createContext, DEFAULT_ID_GENERATOR } from '../context';
import { createContext } from '../oas/context';
import { transformOasOperation, transformOasOperations } from '../oas/operation';
import { resolveRef } from '../oas/resolver';
import { Oas2HttpOperationTransformer } from '../oas/types';
import type { Fragment } from '../types';
import { translateToRequest } from './transformers/request';
Expand All @@ -16,7 +15,7 @@ export function transformOas2Operations(document: DeepPartial<Spec>): IHttpOpera
}

export const transformOas2Operation: Oas2HttpOperationTransformer = ({ document: _document, path, method }) => {
const ctx = createContext(_document, resolveRef, DEFAULT_ID_GENERATOR);
const ctx = createContext(_document);
const httpOperation = transformOasOperation.call(ctx, path, method);
const pathObj = ctx.maybeResolveLocalRef(ctx.document.paths![path]) as Fragment;
const operation = ctx.maybeResolveLocalRef(pathObj[method]) as Fragment;
Expand Down
5 changes: 2 additions & 3 deletions src/oas2/service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { isPlainObject } from '@stoplight/json';
import pickBy = require('lodash.pickby');

import { createContext, DEFAULT_ID_GENERATOR } from '../context';
import { isNonNullable, isString } from '../guards';
import { resolveRef } from '../oas/resolver';
import { createContext } from '../oas/context';
import { transformOasService } from '../oas/service';
import { Oas2HttpServiceTransformer } from '../oas/types';
import { entries } from '../utils';
import { translateToSingleSecurity } from './transformers/securities';
import { translateToServer } from './transformers/servers';

export const transformOas2Service: Oas2HttpServiceTransformer = ({ document }) => {
const ctx = createContext(document, resolveRef, DEFAULT_ID_GENERATOR);
const ctx = createContext(document);
const httpService = transformOasService.call(ctx);

if (document.info?.license) {
Expand Down
67 changes: 33 additions & 34 deletions src/oas2/transformers/__tests__/__snapshots__/params.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -74,46 +74,45 @@ Object {
exports[`params.translator translateToHeaderParams should translate empty dictionary to empty array 1`] = `Array []`;
exports[`params.translator translateToHeaderParams should translate to multiple header params 1`] = `
Array [
Object {
"description": "a description",
"id": "117996bbb6f45",
"name": "header-name",
"schema": Object {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "string",
"x-stoplight-id": "9259b14a1615b",
},
"style": "simple",
Object {
"description": "a description",
"id": "117996bbb6f45",
"name": "header-name",
"schema": Object {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "string",
"x-stoplight-id": Any<String>,
},
Object {
"description": "another description",
"id": "dce8b374eb7d6",
"name": "plain-tex",
"schema": Object {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "string",
"x-stoplight-id": "31f792c907f10",
},
"style": "simple",
"style": "simple",
}
`;
exports[`params.translator translateToHeaderParams should translate to multiple header params 2`] = `
Object {
"description": "another description",
"id": "dce8b374eb7d6",
"name": "plain-tex",
"schema": Object {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "string",
"x-stoplight-id": Any<String>,
},
]
"style": "simple",
}
`;
exports[`params.translator translateToHeaderParams should translate to simple header param 1`] = `
Array [
Object {
"description": "a description",
"id": "117996bbb6f45",
"name": "header-name",
"schema": Object {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "string",
"x-stoplight-id": "9259b14a1615b",
},
"style": "simple",
Object {
"description": "a description",
"id": "117996bbb6f45",
"name": "header-name",
"schema": Object {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "string",
"x-stoplight-id": Any<String>,
},
]
"style": "simple",
}
`;
exports[`params.translator translateToPathParameter should translate 1`] = `
Expand Down
78 changes: 46 additions & 32 deletions src/oas2/transformers/__tests__/params.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { DeepPartial, HttpParamStyles } from '@stoplight/types';
import { FormDataParameter, QueryParameter, Spec } from 'swagger-schema-official';

import { createContext, DEFAULT_ID_GENERATOR } from '../../../context';
import { resolveRef } from '../../../oas/resolver';
import { createContext } from '../../../oas/context';
import {
translateFromFormDataParameters as _translateFromFormDataParameters,
translateToBodyParameter as _translateToBodyParameter,
Expand All @@ -15,28 +14,28 @@ import {
const translateFromFormDataParameters = (
document: DeepPartial<Spec>,
...params: Parameters<typeof _translateFromFormDataParameters>
) => _translateFromFormDataParameters.call(createContext(document, resolveRef, DEFAULT_ID_GENERATOR), ...params);
) => _translateFromFormDataParameters.call(createContext(document), ...params);

const translateToBodyParameter = (
document: DeepPartial<Spec>,
...params: Parameters<typeof _translateToBodyParameter>
) => _translateToBodyParameter.call(createContext(document, resolveRef, DEFAULT_ID_GENERATOR), ...params);
) => _translateToBodyParameter.call(createContext(document), ...params);

const translateToHeaderParam = (document: DeepPartial<Spec>, ...params: Parameters<typeof _translateToHeaderParam>) =>
_translateToHeaderParam.call(createContext(document, resolveRef, DEFAULT_ID_GENERATOR), ...params);
_translateToHeaderParam.call(createContext(document), ...params);

const translateToHeaderParams = (document: DeepPartial<Spec>, ...params: Parameters<typeof _translateToHeaderParams>) =>
_translateToHeaderParams.call(createContext(document, resolveRef, DEFAULT_ID_GENERATOR), ...params);
_translateToHeaderParams.call(createContext(document), ...params);

const translateToPathParameter = (
document: DeepPartial<Spec>,
...params: Parameters<typeof _translateToPathParameter>
) => _translateToPathParameter.call(createContext(document, resolveRef, DEFAULT_ID_GENERATOR), ...params);
) => _translateToPathParameter.call(createContext(document), ...params);

const translateToQueryParameter = (
document: DeepPartial<Spec>,
...params: Parameters<typeof _translateToQueryParameter>
) => _translateToQueryParameter.call(createContext(document, resolveRef, DEFAULT_ID_GENERATOR), ...params);
) => _translateToQueryParameter.call(createContext(document), ...params);

describe('params.translator', () => {
let consumes = ['*'];
Expand Down Expand Up @@ -70,35 +69,50 @@ describe('params.translator', () => {
});

it('should translate to simple header param', () => {
expect(
translateToHeaderParams(
{},
{
'header-name': {
description: 'a description',
type: 'string',
},
const params = translateToHeaderParams(
{},
{
'header-name': {
description: 'a description',
type: 'string',
},
),
).toMatchSnapshot();
},
);

expect(params).toHaveLength(1);
expect(params[0]).toMatchSnapshot({
schema: {
'x-stoplight-id': expect.any(String),
},
});
});

it('should translate to multiple header params', () => {
expect(
translateToHeaderParams(
{},
{
'header-name': {
description: 'a description',
type: 'string',
},
'plain-tex': {
description: 'another description',
type: 'string',
},
const params = translateToHeaderParams(
{},
{
'header-name': {
description: 'a description',
type: 'string',
},
),
).toMatchSnapshot();
'plain-tex': {
description: 'another description',
type: 'string',
},
},
);

expect(params).toHaveLength(2);
expect(params[0]).toMatchSnapshot({
schema: {
'x-stoplight-id': expect.any(String),
},
});
expect(params[1]).toMatchSnapshot({
schema: {
'x-stoplight-id': expect.any(String),
},
});
});
});

Expand Down
5 changes: 2 additions & 3 deletions src/oas2/transformers/__tests__/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
QueryParameter,
} from 'swagger-schema-official';

import { createContext, DEFAULT_ID_GENERATOR } from '../../../context';
import { resolveRef } from '../../../oas/resolver';
import { createContext } from '../../../oas/context';
import {
translateFromFormDataParameters,
translateToBodyParameter,
Expand All @@ -20,7 +19,7 @@ import { translateToRequest as _translateToRequest } from '../request';
jest.mock('../params');

const translateToRequest = (path: Record<string, unknown>, parameters: any[]) => {
const ctx = createContext({ consumes: ['*'], paths: { '/api': { parameters } } }, resolveRef, DEFAULT_ID_GENERATOR);
const ctx = createContext({ consumes: ['*'], paths: { '/api': { parameters } } });
return _translateToRequest.call(ctx, path, { parameters });
};

Expand Down
5 changes: 2 additions & 3 deletions src/oas2/transformers/__tests__/responses.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { DeepPartial } from '@stoplight/types';
import { Operation, Schema, Spec } from 'swagger-schema-official';

import { createContext, DEFAULT_ID_GENERATOR } from '../../../context';
import { resolveRef } from '../../../oas/resolver';
import { createContext } from '../../../oas/context';
import { translateToResponses as _translateToResponses } from '../responses';

const translateToResponses = (document: DeepPartial<Spec>, responses: DeepPartial<Operation['responses']>) =>
_translateToResponses.call(createContext(document, resolveRef, DEFAULT_ID_GENERATOR), { responses });
_translateToResponses.call(createContext(document), { responses });

describe('responses', () => {
const produces = ['application/json', 'application/xml'];
Expand Down
5 changes: 2 additions & 3 deletions src/oas2/transformers/__tests__/securities.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { DeepPartial } from '@stoplight/types';
import { Spec } from 'swagger-schema-official';

import { createContext, DEFAULT_ID_GENERATOR } from '../../../context';
import { resolveRef } from '../../../oas/resolver';
import { createContext } from '../../../oas/context';
import { translateToSecurities as _translateToSecurities } from '../securities';

const translateToSecurities = (document: DeepPartial<Spec>, ...params: Parameters<typeof _translateToSecurities>) =>
_translateToSecurities.call(createContext(document, resolveRef, DEFAULT_ID_GENERATOR), ...params);
_translateToSecurities.call(createContext(document), ...params);

describe('securities', () => {
describe('translateToSecurities', () => {
Expand Down
Loading

0 comments on commit e13dc26

Please sign in to comment.