MCPcopy Index your code
hub / github.com/ReAPI-com/mcp-openapi / parseSpec

Method parseSpec

src/core/SpecScanner.ts:144–205  ·  view source on GitHub ↗

* Parses the spec content based on the file type and converts Swagger 2.0 to OpenAPI 3.0 if needed * @param content - Raw file content * @param fileType - Type of the file (json or yaml) * @returns Parsed and potentially converted spec object

(
    content: string,
    fileType: "json" | "yaml"
  )

Source from the content-addressed store, hash-verified

142 * @returns Parsed and potentially converted spec object
143 */
144 private async parseSpec(
145 content: string,
146 fileType: "json" | "yaml"
147 ): Promise<unknown> {
148 try {
149 const parsedContent =
150 fileType === "json" ? JSON.parse(content) : parse(content);
151
152 // Check if this is a Swagger 2.0 spec
153 if (
154 typeof parsedContent === "object" &&
155 parsedContent !== null &&
156 "swagger" in parsedContent &&
157 parsedContent.swagger === "2.0"
158 ) {
159 // Convert Swagger 2.0 to OpenAPI 3.0
160 const options = {
161 patch: true, // fix up small errors in the source
162 warnOnly: true, // do not throw on non-patchable errors
163 };
164
165 try {
166 const converted = await new Promise<OpenAPIV3.Document>(
167 (resolve, reject) => {
168 swagger2openapi.convertObj(
169 parsedContent,
170 options,
171 (
172 err: Error | null,
173 result: { openapi: OpenAPIV3.Document }
174 ) => {
175 if (err) {
176 reject(
177 new Error(`Swagger 2.0 conversion failed: ${err.message}`)
178 );
179 } else {
180 resolve(result.openapi);
181 }
182 }
183 );
184 }
185 );
186
187 return converted;
188 } catch (error) {
189 throw new Error(
190 `Failed to convert Swagger 2.0 spec: ${
191 error instanceof Error ? error.message : String(error)
192 }`
193 );
194 }
195 }
196
197 return parsedContent;
198 } catch (error) {
199 throw new Error(
200 `Failed to parse ${fileType} content: ${
201 error instanceof Error ? error.message : String(error)

Callers 1

processFileMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected