Skip to content
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
2 changes: 1 addition & 1 deletion docs/ja/README-ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ components:
format: A-and-B
```

Data Type Formatを任意の型定義に変換するオプションは次のように定義します
Data Type Format を任意の型定義に変換するオプションは次のように定義します

```ts
import { CodeGenerator, Option } from "@himenon/openapi-typescript-code-generator";
Expand Down
18 changes: 14 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ export class CodeGenerator {
private rootSchema: Types.OpenApi.Document;
private resolvedReferenceDocument: Types.OpenApi.Document;
private parser: Api.OpenApiTools.Parser;
constructor(private readonly entryPoint: string, private option?: Option) {
this.rootSchema = Api.FileSystem.loadJsonOrYaml(entryPoint);
this.resolvedReferenceDocument = Api.ResolveReference.resolve(entryPoint, entryPoint, JSON.parse(JSON.stringify(this.rootSchema)));
constructor(private readonly entryPointOrDocument: string | Types.OpenApi.Document, private option?: Option) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Himenon TOOD update interface

if (typeof entryPointOrDocument === "string") {
this.rootSchema = Api.FileSystem.loadJsonOrYaml(entryPointOrDocument);
this.resolvedReferenceDocument = Api.ResolveReference.resolve(
entryPointOrDocument,
entryPointOrDocument,
JSON.parse(JSON.stringify(this.rootSchema)),
);
} else {
this.rootSchema = entryPointOrDocument;
this.resolvedReferenceDocument = Api.ResolveReference.resolve(".", ".", JSON.parse(JSON.stringify(this.rootSchema)));
}
this.parser = this.createParser();
}

private createParser(): Api.OpenApiTools.Parser {
return new Api.OpenApiTools.Parser(this.entryPoint, this.rootSchema, this.resolvedReferenceDocument, this.option?.convertOption);
const entryPoint = typeof this.entryPointOrDocument === "string" ? this.entryPointOrDocument : ".";
return new Api.OpenApiTools.Parser(entryPoint, this.rootSchema, this.resolvedReferenceDocument, this.option?.convertOption);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/internal/OpenApiTools/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export class Parser {
private convertContext: ConvertContext.Types;
private store: Store;
private factory: TypeScriptCodeGenerator.Factory.Type;
constructor(private entryPoint: string, private readonly rootSchema: OpenApi.Document, noReferenceOpenApiSchema: OpenApi.Document, convertOption?: ConvertContext.Options) {
constructor(
private entryPoint: string,
private readonly rootSchema: OpenApi.Document,
noReferenceOpenApiSchema: OpenApi.Document,
convertOption?: ConvertContext.Options,
) {
this.currentPoint = entryPoint;
this.factory = TypeScriptCodeGenerator.Factory.create();
this.convertContext = ConvertContext.create(this.factory, convertOption);
Expand Down
47 changes: 47 additions & 0 deletions test/__tests__/client-generate-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as fs from "fs";
import * as path from "path";

import { CodeGenerator } from "../../src";
import * as Templates from "../../src/templates";
import type * as Types from "../../src/types";

describe("raw-json-generate", () => {
test("the raw json input result and the json file path result are same", () => {
const generateCode = JSON.parse(fs.readFileSync(path.join(__dirname, "../kubernetes/openapi-v1.18.5.json"), { encoding: "utf-8" }));

const codeGenerator1 = new CodeGenerator(generateCode);
const codeGenerator2 = new CodeGenerator(path.join(__dirname, "../kubernetes/openapi-v1.18.5.json"));

const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
generator: Templates.ApiClient.generator,
option: {},
};

const code1 = codeGenerator1.generateTypeDefinition([
codeGenerator1.getAdditionalTypeDefinitionCustomCodeGenerator(),
apiClientGeneratorTemplate,
]);

const code2 = codeGenerator2.generateTypeDefinition([
codeGenerator1.getAdditionalTypeDefinitionCustomCodeGenerator(),
apiClientGeneratorTemplate,
]);

expect(code1).toBe(code2);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

});
test("yaml file path loadable", () => {
const codeGenerator = new CodeGenerator(path.join(__dirname, "../api.test.domain/index.yml"));

const apiClientGeneratorTemplate: Types.CodeGenerator.CustomGenerator<Templates.ApiClient.Option> = {
generator: Templates.ApiClient.generator,
option: {},
};

const code = codeGenerator.generateTypeDefinition([
codeGenerator.getAdditionalTypeDefinitionCustomCodeGenerator(),
apiClientGeneratorTemplate,
]);

expect(code).not.toBeFalsy();
});
});
1 change: 0 additions & 1 deletion test/format.domain/index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,3 @@ components:
AandB:
type: string
format: A-and-B