Skip to content

Commit 53065b2

Browse files
authored
feat: CodeGeneratorのentryPointに直接OpenApi.Documentを指定可能にする (#66)
Co-authored-by: tkow <[email protected]>
1 parent 0e7680f commit 53065b2

File tree

5 files changed

+68
-7
lines changed

5 files changed

+68
-7
lines changed

docs/ja/README-ja.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ components:
192192
format: A-and-B
193193
```
194194
195-
Data Type Formatを任意の型定義に変換するオプションは次のように定義します
195+
Data Type Format を任意の型定義に変換するオプションは次のように定義します
196196
197197
```ts
198198
import { CodeGenerator, Option } from "@himenon/openapi-typescript-code-generator";

src/index.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,24 @@ export class CodeGenerator {
1212
private rootSchema: Types.OpenApi.Document;
1313
private resolvedReferenceDocument: Types.OpenApi.Document;
1414
private parser: Api.OpenApiTools.Parser;
15-
constructor(private readonly entryPoint: string, private option?: Option) {
16-
this.rootSchema = Api.FileSystem.loadJsonOrYaml(entryPoint);
17-
this.resolvedReferenceDocument = Api.ResolveReference.resolve(entryPoint, entryPoint, JSON.parse(JSON.stringify(this.rootSchema)));
15+
constructor(private readonly entryPointOrDocument: string | Types.OpenApi.Document, private option?: Option) {
16+
if (typeof entryPointOrDocument === "string") {
17+
this.rootSchema = Api.FileSystem.loadJsonOrYaml(entryPointOrDocument);
18+
this.resolvedReferenceDocument = Api.ResolveReference.resolve(
19+
entryPointOrDocument,
20+
entryPointOrDocument,
21+
JSON.parse(JSON.stringify(this.rootSchema)),
22+
);
23+
} else {
24+
this.rootSchema = entryPointOrDocument;
25+
this.resolvedReferenceDocument = Api.ResolveReference.resolve(".", ".", JSON.parse(JSON.stringify(this.rootSchema)));
26+
}
1827
this.parser = this.createParser();
1928
}
2029

2130
private createParser(): Api.OpenApiTools.Parser {
22-
return new Api.OpenApiTools.Parser(this.entryPoint, this.rootSchema, this.resolvedReferenceDocument, this.option?.convertOption);
31+
const entryPoint = typeof this.entryPointOrDocument === "string" ? this.entryPointOrDocument : ".";
32+
return new Api.OpenApiTools.Parser(entryPoint, this.rootSchema, this.resolvedReferenceDocument, this.option?.convertOption);
2333
}
2434

2535
/**

src/internal/OpenApiTools/Parser.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ export class Parser {
2020
private convertContext: ConvertContext.Types;
2121
private store: Store;
2222
private factory: TypeScriptCodeGenerator.Factory.Type;
23-
constructor(private entryPoint: string, private readonly rootSchema: OpenApi.Document, noReferenceOpenApiSchema: OpenApi.Document, convertOption?: ConvertContext.Options) {
23+
constructor(
24+
private entryPoint: string,
25+
private readonly rootSchema: OpenApi.Document,
26+
noReferenceOpenApiSchema: OpenApi.Document,
27+
convertOption?: ConvertContext.Options,
28+
) {
2429
this.currentPoint = entryPoint;
2530
this.factory = TypeScriptCodeGenerator.Factory.create();
2631
this.convertContext = ConvertContext.create(this.factory, convertOption);
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as fs from "fs";
2+
import * as path from "path";
3+
4+
import { CodeGenerator } from "../../src";
5+
import * as Templates from "../../src/templates";
6+
import type * as Types from "../../src/types";
7+
8+
describe("raw-json-generate", () => {
9+
test("the raw json input result and the json file path result are same", () => {
10+
const generateCode = JSON.parse(fs.readFileSync(path.join(__dirname, "../kubernetes/openapi-v1.18.5.json"), { encoding: "utf-8" }));
11+
12+
const codeGenerator1 = new CodeGenerator(generateCode);
13+
const codeGenerator2 = new CodeGenerator(path.join(__dirname, "../kubernetes/openapi-v1.18.5.json"));
14+
15+
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
16+
generator: Templates.ApiClient.generator,
17+
option: {},
18+
};
19+
20+
const code1 = codeGenerator1.generateTypeDefinition([
21+
codeGenerator1.getAdditionalTypeDefinitionCustomCodeGenerator(),
22+
apiClientGeneratorTemplate,
23+
]);
24+
25+
const code2 = codeGenerator2.generateTypeDefinition([
26+
codeGenerator1.getAdditionalTypeDefinitionCustomCodeGenerator(),
27+
apiClientGeneratorTemplate,
28+
]);
29+
30+
expect(code1).toBe(code2);
31+
});
32+
test("yaml file path loadable", () => {
33+
const codeGenerator = new CodeGenerator(path.join(__dirname, "../api.test.domain/index.yml"));
34+
35+
const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
36+
generator: Templates.ApiClient.generator,
37+
option: {},
38+
};
39+
40+
const code = codeGenerator.generateTypeDefinition([
41+
codeGenerator.getAdditionalTypeDefinitionCustomCodeGenerator(),
42+
apiClientGeneratorTemplate,
43+
]);
44+
45+
expect(code).not.toBeFalsy();
46+
});
47+
});

test/format.domain/index.yml

-1
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,3 @@ components:
6161
AandB:
6262
type: string
6363
format: A-and-B
64-

0 commit comments

Comments
 (0)