Skip to content

Commit 19ff3a3

Browse files
committed
feat: supports fragment in remote reference
1 parent c248b6f commit 19ff3a3

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/internal/FileSystem/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,17 @@ export class FileSystem {
2828
}
2929

3030
public static existSync(entryPoint: string): boolean {
31+
const fragmentIndex = entryPoint.indexOf(this.FRAGMENT);
32+
const hasFragment = fragmentIndex !== -1;
33+
if (hasFragment) {
34+
const filename = entryPoint.substring(0, fragmentIndex);
35+
return !!(fs.existsSync(filename) && fs.statSync(filename).isFile());
36+
}
3137
return !!(fs.existsSync(entryPoint) && fs.statSync(entryPoint).isFile());
3238
}
3339

3440
public static loadJsonOrYaml(entryPoint: string): any {
35-
const hasFragment: boolean = new RegExp(this.FRAGMENT).test(entryPoint);
41+
const hasFragment = entryPoint.indexOf(this.FRAGMENT) !== -1;
3642
if (hasFragment) {
3743
const [filename, fragment] = entryPoint.split(this.FRAGMENT);
3844
const data = this.internalLoadJsonOrYaml(filename);

src/internal/OpenApiTools/components/Reference.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,22 +136,28 @@ export const generate = <T>(entryPoint: string, currentPoint: string, reference:
136136
throw new NotFoundFileError(`Not found reference point from current point. \n Path: ${referencePoint}`);
137137
}
138138

139-
const relativePathFromEntryPoint = path.relative(path.dirname(entryPoint), referencePoint); // components/hoge/fuga.yml
140-
const ext = path.extname(relativePathFromEntryPoint); // .yml
141-
const pathArray: string[] = relativePathFromEntryPoint.replace(ext, "").split(path.sep); // ["components", "hoge", "fuga"]
142-
const targetPath: string = pathArray.join("/"); // components/hoge/fuga
139+
const fragmentIndex = referencePoint.indexOf("#/");
140+
let targetPath: string;
141+
if (fragmentIndex !== -1) {
142+
targetPath = referencePoint.substring(fragmentIndex + 2);
143+
} else {
144+
const relativePathFromEntryPoint = path.relative(path.dirname(entryPoint), referencePoint); // components/hoge/fuga.yml
145+
if (!relativePathFromEntryPoint.startsWith("components")) {
146+
throw new DevelopmentError(`targetPath is not start "components":\n${relativePathFromEntryPoint}`);
147+
}
148+
149+
const ext = path.extname(relativePathFromEntryPoint); // .yml
150+
targetPath = relativePathFromEntryPoint.substring(0, relativePathFromEntryPoint.length - ext.length); // components/hoge/fuga
151+
}
152+
const pathArray: string[] = targetPath.split(path.sep); // ["components", "hoge", "fuga"]
143153
const schemaName = pathArray[pathArray.length - 1]; // fuga
144154
const componentName = pathArray[0] === "components" ? pathArray[1] : "";
145-
const data = FileSystem.loadJsonOrYaml(referencePoint);
146155

156+
const data = FileSystem.loadJsonOrYaml(referencePoint);
147157
if (Guard.isReference(data)) {
148158
return generate<T>(entryPoint, referencePoint, data);
149159
}
150160

151-
if (!targetPath.startsWith("components")) {
152-
throw new DevelopmentError(`targetPath is not start "components":\n${targetPath}`);
153-
}
154-
155161
return {
156162
type: "remote",
157163
referencePoint,

0 commit comments

Comments
 (0)