Skip to content

Commit

Permalink
OpenAPI generator now supports references to external files
Browse files Browse the repository at this point in the history
  • Loading branch information
gius committed Dec 7, 2022
1 parent 021e038 commit 5957740
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ program
config: options.config,
debug: options.debug,
};
console.log("Processing ", params.config);

if (options.decorators) {
params.decorators = { output: options.decoratorsOutput };
Expand Down Expand Up @@ -52,6 +53,7 @@ program
output: options.output,
debug: options.debug,
};
console.log("Processing ", params.config);

const GeneratorType = await import("./views");
const generator = new GeneratorType.default(params);
Expand All @@ -72,6 +74,7 @@ program
config: options.config,
debug: options.debug,
};
console.log("Processing ", params.config);

const GeneratorType = await import("./openapi");
const generator = new GeneratorType.default(params);
Expand Down
1 change: 1 addition & 0 deletions packages/generator/src/openapi/fileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export default class FileGenerator {
unionWriter.write(type);
}
// we can ignore AliasEntity because it is inlined
// we can ignore ExternalEntity because it is external

progress.increment();
}
Expand Down
3 changes: 2 additions & 1 deletion packages/generator/src/openapi/models/entity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type AliasEntity from "./aliasEntity";
import type ExternalEntity from "./externalEntity";
import type InheritedEntity from "./inheritedEntity";
import type ObjectEntity from "./objectEntity";
import type UnionEntity from "./unionEntity";

type Entity = AliasEntity | ObjectEntity | InheritedEntity | UnionEntity;
type Entity = AliasEntity | ObjectEntity | InheritedEntity | UnionEntity | ExternalEntity;

export default Entity;
7 changes: 7 additions & 0 deletions packages/generator/src/openapi/models/externalEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import NamedObject from "./namedObject";

export default class ExternalEntity extends NamedObject {
constructor(name: string, public fullName: string) {
super(name);
}
}
16 changes: 14 additions & 2 deletions packages/generator/src/openapi/parsers/openApi2Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type ApiModel from "../models/apiModel";
import type Endpoint from "../models/endpoint";
import EntityProperty from "../models/entityProperty";
import Enum from "../models/enum";
import ExternalEntity from "../models/externalEntity";
import InheritedEntity from "../models/inheritedEntity";
import ObjectEntity from "../models/objectEntity";
import Restriction from "../models/restriction";
Expand Down Expand Up @@ -86,7 +87,11 @@ export default class OpenApi2Parser implements ApiModel {

private parseReferenceObject(definition: OpenAPIV2.ReferenceObject) {
const name = getReferencedEntityName(definition.$ref);
return this.setTypeReference(name, undefined);
const type = isLocalReference(definition.$ref)
? undefined // it will be set later when the referenced entity is parsed
: new ExternalEntity(name, definition.$ref); // just link the entity by name

return this.setTypeReference(name, type);
}

private parseEnum(name: string, definition: IJsonSchema) {
Expand Down Expand Up @@ -204,6 +209,13 @@ export default class OpenApi2Parser implements ApiModel {
}
}

const REFERENCE_START = "#/definitions/";

function getReferencedEntityName(ref: string) {
return ref.replace("#/definitions/", "");
const index = ref.indexOf(REFERENCE_START, 0);
return index >= 0 ? ref.substring(index + REFERENCE_START.length) : ref;
}

function isLocalReference(ref: string) {
return ref.startsWith(REFERENCE_START);
}
16 changes: 14 additions & 2 deletions packages/generator/src/openapi/parsers/openApi3Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type ApiModel from "../models/apiModel";
import Endpoint from "../models/endpoint";
import EntityProperty from "../models/entityProperty";
import Enum from "../models/enum";
import ExternalEntity from "../models/externalEntity";
import InheritedEntity from "../models/inheritedEntity";
import ObjectEntity from "../models/objectEntity";
import Restriction from "../models/restriction";
Expand Down Expand Up @@ -83,7 +84,11 @@ export default class OpenApi3Parser implements ApiModel {

private parseReferenceObject(definition: OpenAPIV3.ReferenceObject) {
const name = getReferencedEntityName(definition.$ref);
return this.setTypeReference(name, undefined);
const type = isLocalReference(definition.$ref)
? undefined // it will be set later when the referenced entity is parsed
: new ExternalEntity(name, definition.$ref); // just link the entity by name

return this.setTypeReference(name, type);
}

private parseEnum(name: string, definition: OpenAPIV3.SchemaObject) {
Expand Down Expand Up @@ -311,8 +316,15 @@ export default class OpenApi3Parser implements ApiModel {
}
}

const REFERENCE_START = "#/components/schemas/";

function getReferencedEntityName(ref: string) {
return ref.replace("#/components/schemas/", "");
const index = ref.indexOf(REFERENCE_START, 0);
return index >= 0 ? ref.substring(index + REFERENCE_START.length) : ref;
}

function isLocalReference(ref: string) {
return ref.startsWith(REFERENCE_START);
}

function isHttpMethod(method: string) {
Expand Down

0 comments on commit 5957740

Please sign in to comment.