Skip to content

Commit

Permalink
fix: sanitize regex pattern (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon authored Feb 11, 2024
1 parent 95a56fc commit 61399c0
Show file tree
Hide file tree
Showing 18 changed files with 365,329 additions and 12 deletions.
2 changes: 2 additions & 0 deletions scripts/testCodeGenWithClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const main = () => {
Writer.generateParameter("test/infer.domain/index.yml", "test/code/class/parameter/infer.domain.json");

Writer.generateFormatTypeCode("test/format.domain/index.yml", "test/code/class/format.domain/code.ts");

Writer.generateFormatTypeCode("test/cloudflare/openapi.yaml", "test/code/class/cloudflare/client.ts");
};

main();
2 changes: 2 additions & 0 deletions scripts/testCodeGenWithCurryingFunctional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ const main = () => {
Writer.generateParameter("test/infer.domain/index.yml", "test/code/currying-functional/parameter/infer.domain.json");

Writer.generateFormatTypeCode("test/format.domain/index.yml", "test/code/currying-functional/format.domain/code.ts");

Writer.generateFormatTypeCode("test/cloudflare/openapi.yaml", "test/code/currying-functional/cloudflare/client.ts");
};

main();
2 changes: 2 additions & 0 deletions scripts/testCodeGenWithFunctional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ const main = () => {
Writer.generateParameter("test/infer.domain/index.yml", "test/code/functional/parameter/infer.domain.json");

Writer.generateFormatTypeCode("test/format.domain/index.yml", "test/code/functional/format.domain/code.ts");

Writer.generateFormatTypeCode("test/cloudflare/openapi.yaml", "test/code/functional/cloudflare/client.ts");
};

main();
4 changes: 2 additions & 2 deletions src/internal/OpenApiTools/ConverterContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const createFormatSchemaToTypeNode = (factory: Factory.Type, target: FormatConve
export const create = (factory: Factory.Type, options?: Options): Types => {
const convertReservedWord = (word: string): string => {
if (["import", "export"].includes(word)) {
return word + "_";
return `${word}_`;
}
return word;
};
Expand All @@ -104,7 +104,7 @@ export const create = (factory: Factory.Type, options?: Options): Types => {
return text.replace(/-/g, "$").replace(/\//g, "$");
};
const convertOperationId = (text: string): string => {
return convertString(text).replace(/\./g, "$");
return convertString(text).replace(/[^a-zA-Z0-9_]/g, "$");
};
return {
escapeOperationIdText: (operationId: string): string => {
Expand Down
8 changes: 7 additions & 1 deletion src/internal/OpenApiTools/Extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ const extractResponseNamesByStatusCode = (type: "success" | "error", responses:
};

const getRequestContentTypeList = (requestBody: OpenApi.RequestBody): string[] => {
return Object.keys(requestBody.content);
return Object.entries(requestBody.content).reduce<string[]>((list, [key, mediaType]) => {
const hasValidContent = Object.values(mediaType).length > 0;
if (hasValidContent) {
return list.concat(key);
}
return list;
}, []);
};

const getSuccessResponseContentTypeList = (responses: { [statusCode: string]: OpenApi.Response }): string[] => {
Expand Down
5 changes: 4 additions & 1 deletion src/internal/OpenApiTools/Walker/Operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ export const create = (rootSchema: OpenApi.Document): State => {
}
const parameters = [...(pathItem.parameters || []), ...(operation.parameters || [])] as OpenApi.Parameter[];

const requestBody = operation.requestBody as OpenApi.RequestBody | undefined;
const hasValidMediaType = Object.values(requestBody?.content || {}).filter(mediaType => Object.values(mediaType).length > 0).length > 0;

state[operation.operationId] = {
httpMethod,
requestUri,
comment: [operation.summary, operation.description].filter(Boolean).join(EOL),
deprecated: !!operation.deprecated,
requestBody: operation.requestBody as OpenApi.RequestBody,
requestBody: hasValidMediaType ? requestBody : undefined,
parameters: uniqParameters(parameters),
responses: operation.responses as CodeGenerator.OpenApiResponses,
};
Expand Down
21 changes: 15 additions & 6 deletions src/internal/OpenApiTools/components/Operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,21 @@ export const generateStatements = (
});
}
} else {
statements.push(
RequestBody.generateInterface(entryPoint, currentPoint, factory, requestBodyName, operation.requestBody, context, converterContext),
);
store.updateOperationState(httpMethod, requestUri, operationId, {
requestBodyName: requestBodyName,
});
/**
* requestBody:
* content:
* application/json: {}
*/
const hasValidMediaType =
Object.values(operation.requestBody.content).filter(mediaType => Object.values(mediaType).length > 0).length > 0;
if (hasValidMediaType) {
statements.push(
RequestBody.generateInterface(entryPoint, currentPoint, factory, requestBodyName, operation.requestBody, context, converterContext),
);
store.updateOperationState(httpMethod, requestUri, operationId, {
requestBodyName: requestBodyName,
});
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/internal/OpenApiTools/components/RequestBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ export const generateInterface = (
context: ToTypeNode.Context,
converterContext: ConverterContext.Types,
): ts.InterfaceDeclaration => {
/**
* requestBody:
* content:
* application/json: {}
*/
const hasValidMediaType = Object.values(requestBody.content).filter(mediaType => Object.values(mediaType).length > 0).length > 0;
const contentSignatures = MediaType.generatePropertySignatures(
entryPoint,
currentPoint,
factory,
requestBody.content || {},
hasValidMediaType ? requestBody.content : {},
context,
converterContext,
);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const requestContentType = (operationId: string): string => `RequestConte
export const responseContentType = (operationId: string): string => `ResponseContentType$${operationId}`;

export const isAvailableVariableName = (text: string): boolean => {
return /^[A-Za-z_0-9\s]+$/.test(text);
return /^[A-Za-z_0-9]+$/.test(text);
};

export const isFirstCharacterIsValidText = (text: string): boolean => {
Expand Down
Loading

0 comments on commit 61399c0

Please sign in to comment.