Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove old post interpreter and fix tests #795

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/interpreter/Interpreter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommonModel, Draft6Schema, Draft4Schema, SwaggerV2Schema, AsyncapiV2Schema, Draft7Schema } from '../models';
import { interpretName, isEnum, isModelObject } from './Utils';
import { interpretName } from './Utils';
import interpretProperties from './InterpretProperties';
import interpretAllOf from './InterpretAllOf';
import interpretConst from './InterpretConst';
Expand Down Expand Up @@ -91,12 +91,8 @@ export class Interpreter {

interpretNot(schema, model, this, interpreterOptions);

//All schemas of type model object or enum MUST have ids
if (isModelObject(model) === true || isEnum(model) === true) {
model.$id = interpretName(schema) || `anonymSchema${this.anonymCounter++}`;
} else if ((!(schema instanceof Draft4Schema) && schema.$id !== undefined) || (schema instanceof Draft4Schema && schema.id !== undefined)) {
model.$id = interpretName(schema);
}
//All schemas MUST have ids as we do not know how it will be generated and when it will be needed
model.$id = interpretName(schema) || `anonymSchema${this.anonymCounter++}`;
}

/**
Expand Down
67 changes: 0 additions & 67 deletions src/interpreter/PostInterpreter.ts

This file was deleted.

21 changes: 12 additions & 9 deletions src/processors/AsyncAPIInputProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {parse, AsyncAPIDocument, Schema as AsyncAPISchema, ParserOptions} from '@asyncapi/parser';
import { AbstractInputProcessor } from './AbstractInputProcessor';
import { JsonSchemaInputProcessor } from './JsonSchemaInputProcessor';
import { CommonModel, InputMetaModel, ProcessorOptions } from '../models';
import { InputMetaModel, ProcessorOptions } from '../models';
import { Logger } from '../utils';
import { AsyncapiV2Schema } from '../models/AsyncapiV2Schema';
import { convertToMetaModel } from '../helpers';
Expand Down Expand Up @@ -29,16 +29,19 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
doc = input as AsyncAPIDocument;
}
inputModel.originalInput = doc;

//Intermediate model before meta model
let commonModels: {[key: string]: CommonModel} = {};
// Go over all the message payloads and convert them to models
for (const [, message] of doc.allMessages()) {
const schema = AsyncAPIInputProcessor.convertToInternalSchema(message.payload());
const newCommonModels = JsonSchemaInputProcessor.convertSchemaToCommonModel(schema);
commonModels = {...commonModels, ...newCommonModels};
}
for (const [key, commonModel] of Object.entries(commonModels)) {
inputModel.models[String(key)] = convertToMetaModel(commonModel);
const newCommonModel = JsonSchemaInputProcessor.convertSchemaToCommonModel(schema);
if (newCommonModel.$id !== undefined) {
if (inputModel.models[newCommonModel.$id] !== undefined) {
Logger.warn(`Overwriting existing model with $id ${newCommonModel.$id}, are there two models with the same id present?`, newCommonModel);
}
const metaModel = convertToMetaModel(newCommonModel);
inputModel.models[metaModel.name] = metaModel;
} else {
Logger.warn('Model did not have $id which is required, ignoring.', newCommonModel);
}
}
return inputModel;
}
Expand Down
41 changes: 13 additions & 28 deletions src/processors/JsonSchemaInputProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import $RefParser from '@apidevtools/json-schema-ref-parser';
import path from 'path';
import { CommonModel, InputMetaModel, Draft4Schema, Draft7Schema, Draft6Schema, SwaggerV2Schema, OpenapiV3Schema, AsyncapiV2Schema } from '../models';
import { Logger } from '../utils';
import { postInterpretModel } from '../interpreter/PostInterpreter';
import { Interpreter } from '../interpreter/Interpreter';
import { convertToMetaModel } from '../helpers';

Expand Down Expand Up @@ -66,10 +65,9 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
input = JsonSchemaInputProcessor.reflectSchemaNames(input, {}, 'root', true) as Record<string, any>;
await this.dereferenceInputs(input);
const parsedSchema = Draft7Schema.toSchema(input);
const commonModels = JsonSchemaInputProcessor.convertSchemaToCommonModel(parsedSchema);
for (const [key, commonModel] of Object.entries(commonModels)) {
inputModel.models[String(key)] = convertToMetaModel(commonModel);
}
const newCommonModel = JsonSchemaInputProcessor.convertSchemaToCommonModel(parsedSchema);
const metaModel = convertToMetaModel(newCommonModel);
inputModel.models[metaModel.name] = metaModel;
Logger.debug('Completed processing input as JSON Schema draft 7 document');
return inputModel;
}
Expand All @@ -86,10 +84,9 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
input = JsonSchemaInputProcessor.reflectSchemaNames(input, {}, 'root', true) as Record<string, any>;
await this.dereferenceInputs(input);
const parsedSchema = Draft4Schema.toSchema(input);
const commonModels = JsonSchemaInputProcessor.convertSchemaToCommonModel(parsedSchema);
for (const [key, commonModel] of Object.entries(commonModels)) {
inputModel.models[String(key)] = convertToMetaModel(commonModel);
}
const newCommonModel = JsonSchemaInputProcessor.convertSchemaToCommonModel(parsedSchema);
const metaModel = convertToMetaModel(newCommonModel);
inputModel.models[metaModel.name] = metaModel;
Logger.debug('Completed processing input as JSON Schema draft 4 document');
return inputModel;
}
Expand All @@ -106,10 +103,9 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
input = JsonSchemaInputProcessor.reflectSchemaNames(input, {}, 'root', true) as Record<string, any>;
await this.dereferenceInputs(input);
const parsedSchema = Draft6Schema.toSchema(input);
const commonModels = JsonSchemaInputProcessor.convertSchemaToCommonModel(parsedSchema);
for (const [key, commonModel] of Object.entries(commonModels)) {
inputModel.models[String(key)] = convertToMetaModel(commonModel);
}
const newCommonModel = JsonSchemaInputProcessor.convertSchemaToCommonModel(parsedSchema);
const metaModel = convertToMetaModel(newCommonModel);
inputModel.models[metaModel.name] = metaModel;
Logger.debug('Completed processing input as JSON Schema draft 6 document');
return inputModel;
}
Expand Down Expand Up @@ -279,23 +275,12 @@ export class JsonSchemaInputProcessor extends AbstractInputProcessor {
*
* @param schema to simplify to common model
*/
static convertSchemaToCommonModel(schema: Draft4Schema | Draft6Schema | Draft7Schema | SwaggerV2Schema| AsyncapiV2Schema | boolean): Record<string, CommonModel> {
const commonModelsMap: Record<string, CommonModel> = {};
static convertSchemaToCommonModel(schema: Draft4Schema | Draft6Schema | Draft7Schema | SwaggerV2Schema| AsyncapiV2Schema | boolean): CommonModel {
const interpreter = new Interpreter();
const model = interpreter.interpret(schema);
if (model !== undefined) {
const commonModels = postInterpretModel(model);
for (const commonModel of commonModels) {
if (commonModel.$id) {
if (commonModelsMap[commonModel.$id] !== undefined) {
Logger.warn(`Overwriting existing model with $id ${commonModel.$id}, are there two models with the same id present?`, commonModel);
}
commonModelsMap[commonModel.$id] = commonModel;
} else {
Logger.warn('Model did not have $id, ignoring.', commonModel);
}
}
if (model === undefined) {
throw new Error('Could not interpret schema to internal model');
}
return commonModelsMap;
return model;
}
}
12 changes: 9 additions & 3 deletions src/processors/OpenAPIInputProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,15 @@ export class OpenAPIInputProcessor extends AbstractInputProcessor {

private includeSchema(schema: OpenAPIV3.SchemaObject, name: string, inputModel: InputMetaModel) {
const internalSchema = OpenAPIInputProcessor.convertToInternalSchema(schema, name);
const commonModels = JsonSchemaInputProcessor.convertSchemaToCommonModel(internalSchema);
for (const [key, commonModel] of Object.entries(commonModels)) {
inputModel.models[String(key)] = convertToMetaModel(commonModel);
const newCommonModel = JsonSchemaInputProcessor.convertSchemaToCommonModel(internalSchema);
if (newCommonModel.$id !== undefined) {
if (inputModel.models[newCommonModel.$id] !== undefined) {
Logger.warn(`Overwriting existing model with $id ${newCommonModel.$id}, are there two models with the same id present?`, newCommonModel);
}
const metaModel = convertToMetaModel(newCommonModel);
inputModel.models[metaModel.name] = metaModel;
} else {
Logger.warn('Model did not have $id, ignoring.', newCommonModel);
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/processors/SwaggerInputProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@ export class SwaggerInputProcessor extends AbstractInputProcessor {

private includeSchema(schema: OpenAPIV2.SchemaObject, name: string, inputModel: InputMetaModel) {
const internalSchema = SwaggerInputProcessor.convertToInternalSchema(schema, name);
const commonModels = JsonSchemaInputProcessor.convertSchemaToCommonModel(internalSchema);
for (const [key, commonModel] of Object.entries(commonModels)) {
inputModel.models[String(key)] = convertToMetaModel(commonModel);
const newCommonModel = JsonSchemaInputProcessor.convertSchemaToCommonModel(internalSchema);
if (newCommonModel.$id !== undefined) {
if (inputModel.models[newCommonModel.$id] !== undefined) {
Logger.warn(`Overwriting existing model with $id ${newCommonModel.$id}, are there two models with the same id present?`, newCommonModel);
}
const metaModel = convertToMetaModel(newCommonModel);
inputModel.models[metaModel.name] = metaModel;
} else {
Logger.warn('Model did not have $id, ignoring.', newCommonModel);
}
}

Expand Down
20 changes: 12 additions & 8 deletions src/processors/TypeScriptInputProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { CommonModel, InputMetaModel, ProcessorOptions } from '../models';
import { InputMetaModel, ProcessorOptions } from '../models';
import { resolve } from 'path';
import ts from 'typescript';
import * as TJS from 'typescript-json-schema';
import { JsonSchemaInputProcessor } from './JsonSchemaInputProcessor';
import { AbstractInputProcessor } from './AbstractInputProcessor';
import { convertToMetaModel } from '../helpers';
import { Logger } from '../utils';

/** Class for processing Typescript code inputs to Common module*/

export interface TypeScriptInputProcessorOptions extends TJS.PartialArgs{
uniqueNames? : boolean;
required? : boolean;
Expand Down Expand Up @@ -81,13 +81,17 @@ export class TypeScriptInputProcessor extends AbstractInputProcessor {
// obtain generated schema
const generatedSchemas = this.generateJSONSchema(baseFile, '*', options?.typescript);
if (generatedSchemas) {
let commonModels: {[key: string]: CommonModel} = {};
for (const schema of generatedSchemas) {
const newCommonModels = JsonSchemaInputProcessor.convertSchemaToCommonModel(schema as Record<string, any>);
commonModels = {...commonModels, ...newCommonModels };
}
for (const [key, commonModel] of Object.entries(commonModels)) {
inputModel.models[String(key)] = convertToMetaModel(commonModel);
const newCommonModel = JsonSchemaInputProcessor.convertSchemaToCommonModel(schema as Record<string, any>);
if (newCommonModel.$id !== undefined) {
if (inputModel.models[newCommonModel.$id] !== undefined) {
Logger.warn(`Overwriting existing model with $id ${newCommonModel.$id}, are there two models with the same id present?`, newCommonModel);
}
const metaModel = convertToMetaModel(newCommonModel);
inputModel.models[metaModel.name] = metaModel;
} else {
Logger.warn('Model did not have $id, ignoring.', newCommonModel);
}
}
}
return Promise.resolve(inputModel);
Expand Down
Loading