Skip to content

Commit

Permalink
fix(spec-parser): graph spec hang issue, 2XX not support issue (#12865)
Browse files Browse the repository at this point in the history
Co-authored-by: rentu <rentu>
  • Loading branch information
SLdragon authored Dec 6, 2024
1 parent 7d391aa commit 60a09c9
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 57 deletions.
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

0 comments on commit 60a09c9

Please sign in to comment.