Skip to content
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

fix(spec-parser): graph spec hang issue, 2XX not support issue #12865

Merged
merged 1 commit into from
Dec 6, 2024
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 packages/spec-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"dependencies": {
"@apidevtools/swagger-parser": "^10.1.0",
"@microsoft/teams-manifest": "workspace:*",
"@apidevtools/json-schema-ref-parser": "^11.7.2",
"fs-extra": "^11.2.0",
"js-yaml": "^4.1.0",
"openapi-types": "^7.2.3",
Expand All @@ -49,7 +50,6 @@
"access": "public"
},
"devDependencies": {
"@apidevtools/json-schema-ref-parser": "^9.0.6",
"@apidevtools/openapi-schemas": "2.1.0",
"@apidevtools/swagger-methods": "3.0.2",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
Expand Down
17 changes: 13 additions & 4 deletions packages/spec-parser/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/spec-parser/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class ConstantString {
"207",
"208",
"226",
"2XX",
"default",
];
static readonly AllOperationMethods = [
Expand Down
21 changes: 17 additions & 4 deletions packages/spec-parser/src/specParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { wrapAdaptiveCard } from "./adaptiveCardWrapper";
import { ValidatorFactory } from "./validators/validatorFactory";
import { Validator } from "./validators/validator";
import { createHash } from "crypto";
import { $RefParser } from "@apidevtools/json-schema-ref-parser";

/**
* A class that parses an OpenAPI specification file and provides methods to validate, list, and generate artifacts.
Expand All @@ -47,6 +48,8 @@ export class SpecParser {
private unResolveSpec: OpenAPIV3.Document | undefined;
private isSwaggerFile: boolean | undefined;

private readonly refParser;

private defaultOptions: ParseOptions = {
allowMissingId: true,
allowSwagger: true,
Expand All @@ -70,6 +73,7 @@ export class SpecParser {
constructor(pathOrDoc: string | OpenAPIV3.Document, options?: ParseOptions) {
this.pathOrSpec = pathOrDoc;
this.parser = new SwaggerParser();
this.refParser = new $RefParser();
this.options = {
...this.defaultOptions,
...(options ?? {}),
Expand All @@ -87,11 +91,14 @@ export class SpecParser {

try {
await this.loadSpec();
if (!this.parser.$refs.circular) {
if (!this.refParser.$refs.circular) {
await this.parser.validate(this.spec!);
} else {
// The following code hangs for Graph API, support will be added when SwaggerParser is updated.
/*
const clonedUnResolveSpec = JSON.parse(JSON.stringify(this.unResolveSpec));
await this.parser.validate(clonedUnResolveSpec);
*/
}
} catch (e) {
return {
Expand Down Expand Up @@ -122,7 +129,7 @@ export class SpecParser {
}

// Remote reference not supported
const refPaths = this.parser.$refs.paths();
const refPaths = this.refParser.$refs.paths();
// refPaths [0] is the current spec file path
if (refPaths.length > 1) {
errors.push({
Expand Down Expand Up @@ -273,7 +280,7 @@ export class SpecParser {
}

const clonedUnResolveSpec = JSON.parse(JSON.stringify(newUnResolvedSpec));
const newSpec = (await this.parser.dereference(clonedUnResolveSpec)) as OpenAPIV3.Document;
const newSpec = await this.deReferenceSpec(clonedUnResolveSpec);
return [newUnResolvedSpec, newSpec];
} catch (err) {
if (err instanceof SpecParserError) {
Expand All @@ -283,6 +290,11 @@ export class SpecParser {
}
}

private async deReferenceSpec(spec: any): Promise<OpenAPIV3.Document> {
const result = await this.refParser.dereference(spec);
return result as OpenAPIV3.Document;
}

/**
* Generates and update artifacts from the OpenAPI specification file. Generate Adaptive Cards, update Teams app manifest, and generate a new OpenAPI specification file.
* @param manifestPath A file path of the Teams app manifest file to update.
Expand Down Expand Up @@ -479,7 +491,8 @@ export class SpecParser {
}

const clonedUnResolveSpec = JSON.parse(JSON.stringify(this.unResolveSpec));
this.spec = (await this.parser.dereference(clonedUnResolveSpec)) as OpenAPIV3.Document;

this.spec = await this.deReferenceSpec(clonedUnResolveSpec);
}
}

Expand Down
Loading
Loading