Skip to content

fix: schema added support for cases where schemas name contains '.' #57

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

Merged
merged 3 commits into from
Aug 13, 2021
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
4 changes: 4 additions & 0 deletions scripts/testCodeGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ const main = () => {
});
generateTypedefWithTemplateCode("test/infer.domain/index.yml", "test/code/typedef-with-template/infer.domain.ts", false, { sync: false });

generateTypedefWithTemplateCode("test/argo-rollout/index.json", "test/code/typedef-with-template/argo-rollout.ts", false, {
sync: false,
});

generateSplitCode("test/api.test.domain/index.yml", "test/code/split");

generateParameter("test/api.test.domain/index.yml", "test/code/parameter/api.test.domain.json");
Expand Down
9 changes: 9 additions & 0 deletions src/internal/OpenApiTools/ConverterContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export interface Types {
* import/exportなどの予約語も裁く
*/
escapeDeclarationText: (text: string) => string;
/**
* Schemas.A.B.Cに対するEscape
*/
escapeReferenceDeclarationText: (text: string) => string;
/**
* 非破壊: PropertySignatureのname用のescape
*/
Expand Down Expand Up @@ -54,6 +58,11 @@ export const create = (): Types => {
return convertString(operationId);
},
escapeDeclarationText: (text: string) => {
// console.log(`escapeDeclarationText: ${text}` + `-> ${convertReservedWord(convertString(text).replace(/\./g, "$"))}`.padStart(100, " "));
return convertReservedWord(convertString(text).replace(/\./g, "$"));
},
escapeReferenceDeclarationText: (text: string) => {
// console.log(`escapeDeclarationText3: ${text}` + `-> ${convertReservedWord(convertString(text))}`.padStart(100, " "));
return convertReservedWord(convertString(text));
},
escapePropertySignatureName: (text: string) => {
Expand Down
8 changes: 4 additions & 4 deletions src/internal/OpenApiTools/TypeNodeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const generatePath = (entryPoint: string, currentPoint: string, referencePath: s
};
};

const calculateReferencePath = (store: Walker.Store, base: string, pathArray: string[]): ToTypeNode.ResolveReferencePath => {
const calculateReferencePath = (store: Walker.Store, base: string, pathArray: string[], converterContext: ConverterContext.Types,): ToTypeNode.ResolveReferencePath => {
let names: string[] = [];
let unresolvedPaths: string[] = [];
pathArray.reduce((previous, lastPath, index) => {
Expand Down Expand Up @@ -67,8 +67,8 @@ const calculateReferencePath = (store: Walker.Store, base: string, pathArray: st
throw new DevelopmentError("Local Reference Error \n" + JSON.stringify({ pathArray, names, base }, null, 2));
}
return {
name: names.join("."),
maybeResolvedName: names.concat(unresolvedPaths).join("."),
name: names.map(converterContext.escapeDeclarationText).join("."),
maybeResolvedName: names.concat(unresolvedPaths).map(converterContext.escapeDeclarationText).join("."),
unresolvedPaths,
};
};
Expand All @@ -81,7 +81,7 @@ export const create = (
): ToTypeNode.Context => {
const resolveReferencePath: ToTypeNode.Context["resolveReferencePath"] = (currentPoint, referencePath) => {
const { pathArray, base } = generatePath(entryPoint, currentPoint, referencePath);
return calculateReferencePath(store, base, pathArray);
return calculateReferencePath(store, base, pathArray, converterContext);
};
const setReferenceHandler: ToTypeNode.Context["setReferenceHandler"] = (currentPoint, reference) => {
if (store.hasStatement(reference.path, ["interface", "typeAlias"])) {
Expand Down
11 changes: 10 additions & 1 deletion src/internal/OpenApiTools/Walker/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class Store {
});
}
public getRootStatements(): ts.Statement[] {
// fs.writeFileSync("debug/tree.json", JSON.stringify(operator.getHierarchy(), null, 2), { encoding: "utf-8" });
// Debug Point: 抽象的なデータ構造全体を把握するために出力すると良い
// fs.writeFileSync("debug/tree.json", JSON.stringify(this.operator.getHierarchy(), null, 2), { encoding: "utf-8" });
const statements = Def.componentNames.reduce<ts.Statement[]>((statements, componentName) => {
const treeOfNamespace = this.getChildByPaths(componentName, "namespace");
if (treeOfNamespace) {
Expand Down Expand Up @@ -78,6 +79,14 @@ class Store {
throw new UnSupportError(`componentsから始まっていません。path=${path}`);
}
const targetPath = Path.posix.relative("components", path);
// すでにinterfaceとして登録がある場合はスキップ
if (this.hasStatement(targetPath, ["interface"])) {
return;
}
// もしTypeAlias同じスコープに登録されている場合、既存のTypeAliasを削除する
if (this.hasStatement(targetPath, ["typeAlias"])) {
this.operator.remove(targetPath, "typeAlias");
}
this.operator.set(targetPath, Structure.createInstance(statement));
}
public getStatement<T extends Structure.DataStructure.Kind>(path: string, kind: T): Structure.DataStructure.GetChild<T> | undefined {
Expand Down
4 changes: 2 additions & 2 deletions src/internal/OpenApiTools/components/Schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const generateNamespace = (
export: true,
name: convertContext.escapeDeclarationText(name),
type: factory.TypeReferenceNode.create({
name: convertContext.escapeDeclarationText(maybeResolvedName),
name: convertContext.escapeReferenceDeclarationText(maybeResolvedName),
Copy link
Owner Author

Choose a reason for hiding this comment

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

TODO: escapeが二重にかかっているのでシンプルにする

}),
}),
});
Expand Down Expand Up @@ -81,7 +81,7 @@ export const generateNamespace = (
name: convertContext.escapeDeclarationText(name),
comment: reference.data.description,
type: factory.TypeReferenceNode.create({
name: convertContext.escapeDeclarationText(context.resolveReferencePath(currentPoint, reference.path).name),
name: convertContext.escapeReferenceDeclarationText(context.resolveReferencePath(currentPoint, reference.path).name),
Copy link
Owner Author

Choose a reason for hiding this comment

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

TODO: escapeが二重にかかっているのでシンプルにする

}),
}),
});
Expand Down
2 changes: 1 addition & 1 deletion src/internal/OpenApiTools/toTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const convert: Convert = (
// Type Aliasを作成 (or すでにある場合は作成しない)
context.setReferenceHandler(currentPoint, reference);
const { maybeResolvedName } = context.resolveReferencePath(currentPoint, reference.path);
return factory.TypeReferenceNode.create({ name: converterContext.escapeDeclarationText(maybeResolvedName) });
return factory.TypeReferenceNode.create({ name: converterContext.escapeReferenceDeclarationText(maybeResolvedName) });
}
// サポートしているディレクトリに対して存在する場合
if (reference.componentName) {
Expand Down
Loading