Skip to content

Commit

Permalink
apply suggestions, still unable to run e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyfiel committed Dec 22, 2024
1 parent 99dbb67 commit e27090b
Show file tree
Hide file tree
Showing 32 changed files with 155 additions and 147 deletions.
18 changes: 12 additions & 6 deletions packages/cli/src/commands/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ import { isObject, isString, keysOf } from '../utils/js-utils';
import { COMPONENTS, OPENAPI3_METHOD } from './split/types';
import { crawl, startsWithComponents } from './split';

import type { Oas3Definition, Document, Oas3Tag, Referenced } from '@redocly/openapi-core';
import type { Document, Referenced } from '@redocly/openapi-core';
import type { BundleResult } from '@redocly/openapi-core/lib/bundle';
import type {
Oas3Definition,
Oas3_1Definition,
Oas3Parameter,
Oas3PathItem,
Oas3Server,
Oas3_1Definition,
Oas3Tag,
} from '@redocly/openapi-core/lib/typings/openapi';
import type { StrictObject } from '@redocly/openapi-core/lib/utils';
import type { CommandArgs } from '../wrapper';
import type { VerifyConfigOptions } from '../types';

Expand Down Expand Up @@ -310,7 +313,7 @@ export async function handleJoin({
}
}

function collectServers(openapi: Oas3Definition) {
function collectServers(openapi: Oas3Definition | Oas3_1Definition) {
const { servers } = openapi;
if (servers) {
if (!joinedDef.hasOwnProperty('servers')) {
Expand All @@ -324,7 +327,10 @@ export async function handleJoin({
}
}

function collectExternalDocs(openapi: Oas3Definition, { api }: JoinDocumentContext) {
function collectExternalDocs(
openapi: Oas3Definition | Oas3_1Definition,
{ api }: JoinDocumentContext
) {
const { externalDocs } = openapi;
if (externalDocs) {
if (joinedDef.hasOwnProperty('externalDocs')) {
Expand All @@ -338,7 +344,7 @@ export async function handleJoin({
}

function collectPaths(
openapi: Oas3Definition,
openapi: Oas3Definition | Oas3_1Definition,
{
apiFilename,
apiTitle,
Expand Down Expand Up @@ -566,7 +572,7 @@ export async function handleJoin({

function collectWebhooks(
oasVersion: SpecVersion,
openapi: Oas3_1Definition,
openapi: StrictObject<Oas3Definition | Oas3_1Definition>,
{
apiFilename,
apiTitle,
Expand Down
30 changes: 15 additions & 15 deletions packages/cli/src/commands/split/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,18 @@ import {
OPENAPI3_COMPONENT_NAMES,
} from './types';

import type { OasRef } from '@redocly/openapi-core';
import type { Oas3Definition, Oas3_1Definition, Oas2Definition } from '@redocly/openapi-core';
import type {
Definition,
Oas2Definition,
Oas3Schema,
Oas3Definition,
Oas3_1Definition,
Oas3Components,
Oas3_1Schema,
Oas3ComponentsBase,
Oas3_1Components,
Oas3ComponentName,
ComponentsFiles,
RefObject,
Oas3PathItem,
OasRef,
Referenced,
} from './types';
} from '@redocly/openapi-core/lib/typings/openapi';
import type { ComponentsFiles, Definition, RefObject } from './types';
import type { CommandArgs } from '../../wrapper';
import type { VerifyConfigOptions } from '../../types';

Expand Down Expand Up @@ -239,7 +237,7 @@ function doesFileDiffer(filename: string, componentData: any) {

function removeEmptyComponents(
openapi: Oas3Definition | Oas3_1Definition,
componentType: Oas3ComponentName
componentType: Oas3ComponentName<Oas3Schema | Oas3_1Schema>
) {
if (openapi.components && isEmptyObject(openapi.components[componentType])) {
delete openapi.components[componentType];
Expand All @@ -264,15 +262,17 @@ function getFileNamePath(componentDirPath: string, componentName: string, ext: s
}

function gatherComponentsFiles(
components: Oas3Components,
components: Oas3ComponentsBase<Oas3Schema | Oas3_1Schema>| Oas3_1Components,
componentsFiles: ComponentsFiles,
componentType: Oas3ComponentName,
componentType: Oas3ComponentName<Oas3Schema | Oas3_1Schema>,
componentName: string,
filename: string
) {
let inherits: string[] = [];
if (componentType === OPENAPI3_COMPONENT.Schemas) {
inherits = ((components?.[componentType]?.[componentName] as Oas3Schema)?.allOf || [])
inherits = (
(components?.[componentType]?.[componentName] as Oas3Schema | Oas3_1Schema)?.allOf || []
)
.map(({ $ref }) => $ref)
.filter(isTruthy);
}
Expand Down Expand Up @@ -347,7 +347,7 @@ function iterateComponents(
componentTypes.forEach(iterateComponentTypes);

// eslint-disable-next-line no-inner-declarations
function iterateAndGatherComponentsFiles(componentType: Oas3ComponentName) {
function iterateAndGatherComponentsFiles(componentType: Oas3ComponentName<Oas3Schema | Oas3_1Schema>) {
const componentDirPath = path.join(componentsDir, componentType);
for (const componentName of Object.keys(components?.[componentType] || {})) {
const filename = getFileNamePath(componentDirPath, componentName, ext);
Expand All @@ -356,7 +356,7 @@ function iterateComponents(
}

// eslint-disable-next-line no-inner-declarations
function iterateComponentTypes(componentType: Oas3ComponentName) {
function iterateComponentTypes(componentType: Oas3ComponentName<Oas3Schema | Oas3_1Schema>) {
const componentDirPath = path.join(componentsDir, componentType);
createComponentDir(componentDirPath, componentType);
for (const componentName of Object.keys(components?.[componentType] || {})) {
Expand Down
29 changes: 3 additions & 26 deletions packages/cli/src/commands/split/types.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
import {
Oas3Schema,
Oas3_1Schema,
Oas3Definition,
Oas3_1Definition,
Oas3Components,
Oas3PathItem,
Oas3Paths,
Oas3ComponentName,
Oas3_1Webhooks,
Oas2Definition,
Referenced,
} from '@redocly/openapi-core';
export {
Oas3_1Definition,
Oas3Definition,
Oas2Definition,
Oas3Components,
Oas3Paths,
Oas3PathItem,
Oas3ComponentName,
Oas3_1Schema,
Oas3Schema,
Oas3_1Webhooks,
Referenced,
};
import type { Oas2Definition } from '@redocly/openapi-core';
import type { Oas3_1Definition, Oas3Definition } from 'core/src/typings/openapi';

export type Definition = Oas3_1Definition | Oas3Definition | Oas2Definition;
export interface ComponentsFiles {
[schemas: string]: any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { yamlAndJsonSyncReader } from '../../utils';
import { isRef } from '../../ref-utils';

import type { Oas3Decorator } from '../../visitors';
import type { Oas3_1Schema, Oas3Operation, Oas3RequestBody, Oas3Response, Oas3Schema } from '../../typings/openapi';
import type { Oas3Operation, Oas3RequestBody, Oas3Response } from '../../typings/openapi';
import type { NonUndefined, ResolveFn, UserContext } from '../../walk';

export const MediaTypeExamplesOverride: Oas3Decorator = ({ operationIds }) => {
return {
Operation: {
enter(operation: Oas3Operation<Oas3Schema | Oas3_1Schema>, ctx: UserContext) {
enter(operation: Oas3Operation, ctx: UserContext) {
const operationId = operation.operationId;

if (!operationId) {
Expand All @@ -23,7 +23,7 @@ export const MediaTypeExamplesOverride: Oas3Decorator = ({ operationIds }) => {

if (properties.responses && operation.responses) {
for (const responseCode of Object.keys(properties.responses)) {
const resolvedResponse = checkAndResolveRef<Oas3Response<Oas3Schema | Oas3_1Schema>>(
const resolvedResponse = checkAndResolveRef<Oas3Response>(
operation.responses[responseCode],
ctx.resolve
);
Expand All @@ -46,7 +46,7 @@ export const MediaTypeExamplesOverride: Oas3Decorator = ({ operationIds }) => {
}

if (properties.request && operation.requestBody) {
const resolvedRequest = checkAndResolveRef<Oas3RequestBody<Oas3Schema | Oas3_1Schema>>(
const resolvedRequest = checkAndResolveRef<Oas3RequestBody>(
operation.requestBody,
ctx.resolve
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { readFileAsStringSync } from '../../utils';

import type { Oas3Decorator, Oas2Decorator } from '../../visitors';
import type { Oas2Operation } from '../../typings/swagger';
import type { Oas3Schema, Oas3_1Schema, Oas3Operation } from '../../typings/openapi';
import type { Oas3Operation } from '../../typings/openapi';
import type { UserContext } from '../../walk';

export const OperationDescriptionOverride: Oas3Decorator | Oas2Decorator = ({ operationIds }) => {
return {
Operation: {
leave(operation: Oas2Operation | Oas3Operation<Oas3Schema | Oas3_1Schema>, { report, location }: UserContext) {
leave(operation: Oas2Operation | Oas3Operation, { report, location }: UserContext) {
if (!operation.operationId) return;
if (!operationIds)
throw new Error(
Expand Down
22 changes: 18 additions & 4 deletions packages/core/src/decorators/oas3/remove-unused-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,28 @@ import { isEmptyObject } from '../../utils';

import type { Location } from '../../ref-utils';
import type { Oas3Decorator } from '../../visitors';
import type { Oas3Schema, Oas3_1Schema, Oas3Components, Oas3Definition } from '../../typings/openapi';
import type {
Oas3Definition,
Oas3_1Definition,
Oas3ComponentsBase,
Oas3_1Components,
Oas3Schema,
Oas3_1Schema,
} from '../../typings/openapi';

export const RemoveUnusedComponents: Oas3Decorator = () => {
const components = new Map<
string,
{ usedIn: Location[]; componentType?: keyof Oas3Components<Oas3Schema | Oas3_1Schema>; name: string }
{
usedIn: Location[];
componentType?: keyof (Oas3ComponentsBase<Oas3Schema | Oas3_1Schema>| Oas3_1Components);
name: string;
}
>();

function registerComponent(
location: Location,
componentType: keyof Oas3Components<Oas3Schema | Oas3_1Schema>,
componentType: keyof (Oas3ComponentsBase<Oas3Schema | Oas3_1Schema> | Oas3_1Components),
name: string
): void {
components.set(location.absolutePointer, {
Expand All @@ -22,7 +33,10 @@ export const RemoveUnusedComponents: Oas3Decorator = () => {
});
}

function removeUnusedComponents(root: Oas3Definition<Oas3Schema | Oas3_1Schema>, removedPaths: string[]): number {
function removeUnusedComponents(
root: Oas3Definition | Oas3_1Definition,
removedPaths: string[]
): number {
const removedLengthStart = removedPaths.length;

for (const [path, { usedIn, name, componentType }] of components) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ export { ConfigTypes } from './types/redocly-yaml';
export type {
Oas3Definition,
Oas3_1Definition,
Oas3Components,
Oas3ComponentsBase,
Oas3_1Components,
Oas3PathItem,
Oas3Paths,
Oas3ComponentName,
Oas3Schema,
Oas3_1Schema,
Oas3Tag,
Oas3_1Webhooks,
Referenced,
OasRef,
} from './typings/openapi';
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/rules/common/no-ambiguous-paths.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Oas3Rule, Oas2Rule } from '../../visitors';
import type { UserContext } from '../../walk';
import type { Oas3_1Schema, Oas3Paths, Oas3Schema } from '../../typings/openapi';
import type { Oas3Paths } from '../../typings/openapi';
import type { Oas2Paths } from '../../typings/swagger';

export const NoAmbiguousPaths: Oas3Rule | Oas2Rule = () => {
return {
Paths(pathMap: Oas3Paths<Oas3Schema | Oas3_1Schema> | Oas2Paths, { report, location }: UserContext) {
Paths(pathMap: Oas3Paths | Oas2Paths, { report, location }: UserContext) {
const seenPaths: string[] = [];

for (const currentPath of Object.keys(pathMap)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/rules/common/no-http-verbs-in-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { isPathParameter, splitCamelCaseIntoWords } from '../../utils';

import type { Oas3Rule, Oas2Rule } from '../../visitors';
import type { Oas2PathItem } from '../../typings/swagger';
import type { Oas3Schema, Oas3_1Schema, Oas3PathItem } from '../../typings/openapi';
import type { Oas3PathItem } from '../../typings/openapi';
import type { UserContext } from '../../walk';

const httpMethods = ['get', 'head', 'post', 'put', 'patch', 'delete', 'options', 'trace'];

export const NoHttpVerbsInPaths: Oas3Rule | Oas2Rule = ({ splitIntoWords }) => {
return {
PathItem(_path: Oas2PathItem | Oas3PathItem<Oas3Schema | Oas3_1Schema>, { key, report, location }: UserContext) {
PathItem(_path: Oas2PathItem | Oas3PathItem, { key, report, location }: UserContext) {
const pathKey = key.toString();
if (!pathKey.startsWith('/')) return;
const pathSegments = pathKey.split('/');
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/rules/common/no-identical-paths.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Oas3Rule, Oas2Rule } from '../../visitors';
import type { UserContext } from '../../walk';
import type { Oas3Schema, Oas3_1Schema, Oas3Paths } from '../../typings/openapi';
import type { Oas3Paths } from '../../typings/openapi';
import type { Oas2Paths } from '../../typings/swagger';

export const NoIdenticalPaths: Oas3Rule | Oas2Rule = () => {
return {
Paths(pathMap: Oas3Paths<Oas3Schema | Oas3_1Schema> | Oas2Paths, { report, location }: UserContext) {
Paths(pathMap: Oas3Paths | Oas2Paths, { report, location }: UserContext) {
const Paths = new Map<string, string>();
for (const pathName of Object.keys(pathMap)) {
const id = pathName.replace(/{.+?}/g, '{VARIABLE}');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { getAdditionalPropertiesOption, validateExample } from '../utils';

import type { UserContext } from '../../walk';
import type { Oas3Schema, Oas3_1Schema, Oas3Parameter } from '../../typings/openapi';
import type { Oas3Parameter } from '../../typings/openapi';

export const NoInvalidParameterExamples: any = (opts: any) => {
const allowAdditionalProperties = getAdditionalPropertiesOption(opts) ?? false;
return {
Parameter: {
leave(parameter: Oas3Parameter<Oas3Schema | Oas3_1Schema>, ctx: UserContext) {
leave(parameter: Oas3Parameter, ctx: UserContext) {
if (parameter.example !== undefined) {
validateExample(
parameter.example,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/rules/common/operation-description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { validateDefinedAndNonEmpty } from '../utils';
import type { Oas3Rule, Oas2Rule } from '../../visitors';
import type { UserContext } from '../../walk';
import type { Oas2Operation } from '../../typings/swagger';
import type { Oas3Operation, Oas3Schema, Oas3_1Schema } from '../../typings/openapi';
import type { Oas3Operation } from '../../typings/openapi';

export const OperationDescription: Oas3Rule | Oas2Rule = () => {
return {
Operation(operation: Oas2Operation | Oas3Operation<Oas3Schema | Oas3_1Schema>, ctx: UserContext) {
Operation(operation: Oas2Operation | Oas3Operation, ctx: UserContext) {
validateDefinedAndNonEmpty('description', operation, ctx);
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { Oas3Rule, Oas2Rule } from '../../visitors';
import type { Oas2Operation } from '../../typings/swagger';
import type { Oas3Operation, Oas3Schema, Oas3_1Schema } from '../../typings/openapi';
import type { Oas3Operation } from '../../typings/openapi';
import type { UserContext } from '../../walk';

export const OperationIdUnique: Oas3Rule | Oas2Rule = () => {
const seenOperations = new Set();

return {
Operation(operation: Oas2Operation | Oas3Operation<Oas3Schema | Oas3_1Schema>, { report, location }: UserContext) {
Operation(operation: Oas2Operation | Oas3Operation, { report, location }: UserContext) {
if (!operation.operationId) return;
if (seenOperations.has(operation.operationId)) {
report({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { Oas3Rule, Oas2Rule } from '../../visitors';
import type { Oas2Operation } from '../../typings/swagger';
import type { Oas3Operation, Oas3Schema, Oas3_1Schema } from '../../typings/openapi';
import type { Oas3Operation } from '../../typings/openapi';
import type { UserContext } from '../../walk';

// eslint-disable-next-line no-useless-escape
const validUrlSymbols = /^[A-Za-z0-9-._~:/?#\[\]@!\$&'()*+,;=]*$/;

export const OperationIdUrlSafe: Oas3Rule | Oas2Rule = () => {
return {
Operation(operation: Oas2Operation | Oas3Operation<Oas3Schema | Oas3_1Schema>, { report, location }: UserContext) {
Operation(operation: Oas2Operation | Oas3Operation, { report, location }: UserContext) {
if (operation.operationId && !validUrlSymbols.test(operation.operationId)) {
report({
message: 'Operation `operationId` should not have URL invalid characters.',
Expand Down
Loading

0 comments on commit e27090b

Please sign in to comment.