Skip to content

Commit

Permalink
feat(openapi,openapi-client): use openapi v3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrimolet committed Sep 25, 2023
1 parent 2854e8f commit 0ee283b
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/openapi-client/lib/core-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
ParameterObject,
PathItemObject,
OperationObject
} from "openapi3-ts/oas30";
} from "openapi3-ts/oas31";

import {
ParserContext,
Expand Down Expand Up @@ -101,7 +101,7 @@ export function generateDefaults(file: ts.SourceFile, context: OApiGeneratorCont
}

export function generateFunctions(file: ts.SourceFile, spec: OpenAPIObject, context: OApiGeneratorContext): ts.SourceFile {
const paths: typeof spec.paths = Object.fromEntries(Object.entries(spec.paths)
const paths: typeof spec.paths = Object.fromEntries(Object.entries(spec.paths ?? {})
.filter(([path]) => !context.options.prefix || path.startsWith(context.options.prefix)));

const functions: ts.FunctionDeclaration[] = Object.entries(paths).map(([path, pathSpec]) => {
Expand Down
10 changes: 5 additions & 5 deletions packages/openapi-client/lib/core-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
ResponseObject,
ResponsesObject,
ContentObject
} from "openapi3-ts/oas30";
} from "openapi3-ts/oas31";

import {
ParserContext,
Expand Down Expand Up @@ -101,8 +101,8 @@ function parseResponses(result: ParsedOperation, path: string, method: string, o

function parseParameters(result: ParsedOperation, item: PathItemObject, operation: OperationObject, context: OApiGeneratorContext): void {
const parameters = fixDeepObjects([
...resolveReferences(item.parameters, context),
...resolveReferences(operation.parameters, context),
...resolveReferences<ParameterObject>(item.parameters, context),
...resolveReferences<ParameterObject>(operation.parameters, context),
]);

result.query = parameters.filter((p) => p.in === "query");
Expand Down Expand Up @@ -150,7 +150,7 @@ function parseParameters(result: ParsedOperation, item: PathItemObject, operatio
}

function parseRequestBody(result: ParsedOperation, requestBody: RequestBodyObject | ReferenceObject, context: OApiGeneratorContext): void {
const body = resolveReference(requestBody, context);
const body = resolveReference<RequestBodyObject>(requestBody, context);
const [schema, mode] = getSchemaFromContent(body.content);
const type = getTypeFromSchema(schema as JSONSchema, context);

Expand Down Expand Up @@ -260,7 +260,7 @@ export function isMethod(method: string): method is Method {
//#region Private

function getTypeFromResponse(res: ResponseObject | ReferenceObject, context: OApiGeneratorContext): ts.TypeNode {
res = resolveReference(res, context);
res = resolveReference<ResponseObject>(res, context);

if (!res?.content) {
return core.keywordType.void;
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-client/lib/openapi-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import $RefParser from "@apidevtools/json-schema-ref-parser";

import type {
OpenAPIObject
} from "openapi3-ts/oas30";
} from "openapi3-ts/oas31";

import {
ParserOptions,
Expand Down
2 changes: 1 addition & 1 deletion packages/openapi-client/lib/server-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as core from "@spec2ts/core";
import type {
ServerObject,
ServerVariableObject
} from "openapi3-ts/oas30";
} from "openapi3-ts/oas31";

import { camelCase } from "./util";

Expand Down
13 changes: 7 additions & 6 deletions packages/openapi/lib/core-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import type {
OperationObject,
ParameterObject,
ContentObject,
ResponseObject
} from "openapi3-ts/oas30";
ResponseObject,
RequestBodyObject,
} from "openapi3-ts/oas31";

import {
JSONSchema,
Expand All @@ -21,7 +22,7 @@ import {

createRefContext,
resolveReference,
pascalCase
pascalCase,
} from "@spec2ts/jsonschema/lib/core-parser";

import type { ParseOpenApiOptions } from "./openapi-parser";
Expand Down Expand Up @@ -65,7 +66,7 @@ export function parseOperation(path: string, verb: string, operation: OperationO
}

if (operation.requestBody) {
const requestBody = resolveReference(operation.requestBody, context);
const requestBody = resolveReference<RequestBodyObject>(operation.requestBody, context);
const decla = getContentDeclaration(name + "Body", requestBody.content, context);
if (decla) { addToOpenApiResult(result, "body", decla); }
}
Expand All @@ -92,7 +93,7 @@ export function parseParameters(baseName: string, data: Array<ReferenceObject |
const res: ParsedParams = {}

data.forEach(item => {
item = resolveReference(item, context);
item = resolveReference<ParameterObject>(item, context);

switch (item.in) {
case "path":
Expand Down Expand Up @@ -154,7 +155,7 @@ export function parseReference(ref: ParsedReference, context: ParserContext): vo
export function getContentDeclaration(name: string, content: ReferenceObject | ContentObject | undefined, context: OApiParserContext): ts.Statement | undefined {
if (!content) return;

content = resolveReference(content, context);
content = resolveReference<ContentObject>(content, context);

const schema = getSchemaFromContent(content);
if (!schema) return;
Expand Down
4 changes: 2 additions & 2 deletions packages/openapi/lib/openapi-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import $RefParser from "@apidevtools/json-schema-ref-parser";

import type {
OpenAPIObject,
} from "openapi3-ts/oas30";
} from "openapi3-ts/oas31";

import {
ParserOptions,
Expand Down Expand Up @@ -45,7 +45,7 @@ export async function parseOpenApi(spec: OpenAPIObject, options: ParseOpenApiOpt
const context = await createContext(spec, options);
const result: ParseOpenApiResult = createOpenApiResult();

Object.entries(spec.paths).forEach(([path, item]) => {
Object.entries(spec.paths ?? {}).forEach(([path, item]) => {
parsePathItem(path, item, context, result);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/openapi/tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from "path";
import { readFileSync } from "fs";

import type { OpenAPIObject } from "openapi3-ts/oas30";
import type { OpenAPIObject } from "openapi3-ts/oas31";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const jsYaml = require("js-yaml");
Expand Down

0 comments on commit 0ee283b

Please sign in to comment.