Skip to content

Commit 2d0fc2f

Browse files
authored
feat: pass options to processors (#328)
1 parent 8d08031 commit 2d0fc2f

File tree

7 files changed

+46
-21
lines changed

7 files changed

+46
-21
lines changed

src/generators/AbstractGenerator.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { CommonInputModel, CommonModel, OutputModel, Preset, Presets } from '../models';
1+
import { CommonInputModel, CommonModel, OutputModel, Preset, Presets, RenderOutput, ProcessorOptions } from '../models';
22
import { InputProcessor } from '../processors';
33
import { IndentationTypes } from '../helpers';
44
import { isPresetWithOptions } from '../utils';
5-
import { RenderOutput } from '../models/RenderOutput';
6-
75
export interface CommonGeneratorOptions<P extends Preset = Preset> {
86
indentation?: {
97
type: IndentationTypes;
108
size: number;
119
};
1210
defaultPreset?: P;
1311
presets?: Presets<P>;
12+
processorOptions?: ProcessorOptions
1413
}
1514

1615
export const defaultGeneratorOptions: CommonGeneratorOptions = {
@@ -36,8 +35,8 @@ export abstract class AbstractGenerator<Options extends CommonGeneratorOptions =
3635

3736
public abstract render(model: CommonModel, inputModel: CommonInputModel): Promise<RenderOutput>;
3837

39-
public async process(input: Record<string, unknown>): Promise<CommonInputModel> {
40-
return await InputProcessor.processor.process(input);
38+
public process(input: Record<string, unknown>): Promise<CommonInputModel> {
39+
return InputProcessor.processor.process(input, this.options.processorOptions);
4140
}
4241

4342
public async generate(input: Record<string, unknown> | CommonInputModel): Promise<OutputModel[]> {

src/models/ProcessorOptions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export interface AsyncapiProcessorOptions {
2+
path?: string
3+
parse?: any;
4+
resolve?: any;
5+
applyTraits?: any;
6+
}
7+
export interface ProcessorOptions {
8+
asyncapi?: AsyncapiProcessorOptions
9+
}

src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ export * from './CommonSchema';
55
export * from './OutputModel';
66
export * from './Preset';
77
export * from './Schema';
8+
export * from './ProcessorOptions';
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { CommonInputModel } from '../models/CommonInputModel';
2-
1+
import { ProcessorOptions, CommonInputModel } from '../models';
32
export abstract class AbstractInputProcessor {
43
public static MODELGEN_INFFERED_NAME = 'x-modelgen-inferred-name';
5-
abstract process(input: Record<string, any>): Promise<CommonInputModel>;
4+
abstract process(input: Record<string, any>, options?: ProcessorOptions): Promise<CommonInputModel>;
65
abstract shouldProcess(input: Record<string, any>): boolean;
76
}

src/processors/AsyncAPIInputProcessor.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
import {parse, AsyncAPIDocument, Schema as AsyncAPISchema} from '@asyncapi/parser';
2-
32
import { AbstractInputProcessor } from './AbstractInputProcessor';
43
import { JsonSchemaInputProcessor } from './JsonSchemaInputProcessor';
5-
import { CommonInputModel, Schema } from '../models';
4+
import { CommonInputModel, ProcessorOptions, Schema } from '../models';
65
import { Logger } from '../utils';
76

87
/**
98
* Class for processing AsyncAPI inputs
109
*/
1110
export class AsyncAPIInputProcessor extends AbstractInputProcessor {
1211
static supportedVersions = ['2.0.0', '2.1.0'];
12+
1313
/**
1414
* Process the input as an AsyncAPI document
1515
*
1616
* @param input
1717
*/
18-
async process(input: Record<string, any>): Promise<CommonInputModel> {
18+
async process(input: Record<string, any>, options?: ProcessorOptions): Promise<CommonInputModel> {
1919
if (!this.shouldProcess(input)) {throw new Error('Input is not an AsyncAPI document so it cannot be processed.');}
2020

2121
Logger.debug('Processing input as an AsyncAPI document');
2222
let doc: AsyncAPIDocument;
2323
const common = new CommonInputModel();
2424
if (!AsyncAPIInputProcessor.isFromParser(input)) {
25-
doc = await parse(input as any);
25+
doc = await parse(input as any, options?.asyncapi);
2626
} else {
2727
doc = input as AsyncAPIDocument;
2828
}

src/processors/InputProcessor.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AbstractInputProcessor } from './AbstractInputProcessor';
22
import { AsyncAPIInputProcessor } from './AsyncAPIInputProcessor';
33
import { JsonSchemaInputProcessor } from './JsonSchemaInputProcessor';
4-
import { CommonInputModel } from '../models/CommonInputModel';
4+
import { ProcessorOptions, CommonInputModel } from '../models';
55

66
/**
77
* Main input processor which figures out the type of input it receives and delegates the processing into separate individual processors.
@@ -37,18 +37,18 @@ export class InputProcessor {
3737
* The processor code which delegates the processing to the correct implementation.
3838
*
3939
* @param input to process
40-
* @param type of processor to use
40+
* @param options passed to the processors
4141
*/
42-
process(input: Record<string, any>): Promise<CommonInputModel> {
42+
process(input: Record<string, any>, options?: ProcessorOptions): Promise<CommonInputModel> {
4343
for (const [type, processor] of this.processors) {
4444
if (type === 'default') {continue;}
4545
if (processor.shouldProcess(input)) {
46-
return processor.process(input);
46+
return processor.process(input, options);
4747
}
4848
}
4949
const defaultProcessor = this.processors.get('default');
5050
if (defaultProcessor !== undefined) {
51-
return defaultProcessor.process(input);
51+
return defaultProcessor.process(input, options);
5252
}
5353
return Promise.reject(new Error('No default processor found'));
5454
}

test/processors/InputProcessor.spec.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3-
import { CommonInputModel } from '../../src/models';
3+
import { CommonInputModel, ProcessorOptions } from '../../src/models';
44
import { InputProcessor } from '../../src/processors/InputProcessor';
55
import { JsonSchemaInputProcessor } from '../../src/processors/JsonSchemaInputProcessor';
66
import { AsyncAPIInputProcessor } from '../../src/processors/AsyncAPIInputProcessor';
77
import { AbstractInputProcessor } from '../../src/processors';
8-
8+
import AsyncAPIParser from '@asyncapi/parser';
99
describe('InputProcessor', () => {
1010
beforeEach(() => {
1111
jest.resetAllMocks();
@@ -64,7 +64,7 @@ describe('InputProcessor', () => {
6464
await processor.process(inputSchema);
6565
expect(asyncInputProcessor.process).not.toHaveBeenCalled();
6666
expect(asyncInputProcessor.shouldProcess).toHaveBeenNthCalledWith(1, inputSchema);
67-
expect(defaultInputProcessor.process).toHaveBeenNthCalledWith(1, inputSchema);
67+
expect(defaultInputProcessor.process).toHaveBeenNthCalledWith(1, inputSchema, undefined);
6868
expect(defaultInputProcessor.shouldProcess).toHaveBeenNthCalledWith(1, inputSchema);
6969
});
7070

@@ -73,8 +73,25 @@ describe('InputProcessor', () => {
7373
const inputSchemaString = fs.readFileSync(path.resolve(__dirname, './AsyncAPIInputProcessor/basic.json'), 'utf8');
7474
const inputSchema = JSON.parse(inputSchemaString);
7575
await processor.process(inputSchema);
76-
expect(asyncInputProcessor.process).toHaveBeenNthCalledWith(1, inputSchema);
76+
expect(asyncInputProcessor.process).toHaveBeenNthCalledWith(1, inputSchema, undefined);
77+
expect(asyncInputProcessor.shouldProcess).toHaveBeenNthCalledWith(1, inputSchema);
78+
expect(defaultInputProcessor.process).not.toHaveBeenCalled();
79+
expect(defaultInputProcessor.shouldProcess).not.toHaveBeenCalled();
80+
});
81+
test('should be able to process AsyncAPI schema input with options', async () => {
82+
const {processor, asyncInputProcessor, defaultInputProcessor} = getProcessors();
83+
const spy = jest.spyOn(AsyncAPIParser, 'parse');
84+
const options: ProcessorOptions = {
85+
asyncapi: {
86+
path: 'test'
87+
}
88+
};
89+
const inputSchemaString = fs.readFileSync(path.resolve(__dirname, './AsyncAPIInputProcessor/basic.json'), 'utf8');
90+
const inputSchema = JSON.parse(inputSchemaString);
91+
await processor.process(inputSchema, options);
92+
expect(asyncInputProcessor.process).toHaveBeenNthCalledWith(1, inputSchema, options);
7793
expect(asyncInputProcessor.shouldProcess).toHaveBeenNthCalledWith(1, inputSchema);
94+
expect(spy).toHaveBeenNthCalledWith(1, inputSchema, expect.objectContaining(options.asyncapi));
7895
expect(defaultInputProcessor.process).not.toHaveBeenCalled();
7996
expect(defaultInputProcessor.shouldProcess).not.toHaveBeenCalled();
8097
});

0 commit comments

Comments
 (0)