Skip to content

Commit

Permalink
feat: If the type cannot be estimated, set it to any (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon authored Jan 27, 2022
1 parent 735537c commit b876a7c
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 16 deletions.
3 changes: 3 additions & 0 deletions scripts/testCodeGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const main = () => {
Writer.generateTypedefWithTemplateCode("test/argo-rollout/index.json", "test/code/argo-rollout/client.ts", false, {
sync: false,
});
Writer.generateTypedefWithTemplateCode("test/unknown.schema.domain/index.yml", "test/code/unknown.schema.domain/client.ts", false, {
sync: false,
});

Writer.generateSplitCode("test/api.test.domain/index.yml", "test/code/split");

Expand Down
19 changes: 11 additions & 8 deletions src/internal/OpenApiTools/ConverterContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,12 @@ 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 {
escapeOperationIdText: (operationId: string): string => {
return convertString(operationId);
return convertOperationId(operationId);
},
escapeDeclarationText: (text: string) => {
// console.log(`escapeDeclarationText: ${text}` + `-> ${convertReservedWord(convertString(text).replace(/\./g, "$"))}`.padStart(100, " "));
Expand All @@ -122,25 +125,25 @@ export const create = (factory: Factory.Type, options?: Options): Types => {
return convertString(text);
},
generateResponseName: (operationId: string, statusCode: string): string => {
return Utils.responseName(convertString(operationId), statusCode);
return Utils.responseName(convertOperationId(operationId), statusCode);
},
generateArgumentParamsTypeDeclaration: (operationId: string) => {
return Utils.argumentParamsTypeDeclaration(convertString(operationId));
return Utils.argumentParamsTypeDeclaration(convertOperationId(operationId));
},
generateRequestContentTypeName: (operationId: string) => {
return Utils.requestContentType(convertString(operationId));
return Utils.requestContentType(convertOperationId(operationId));
},
generateResponseContentTypeName: (operationId: string) => {
return Utils.responseContentType(convertString(operationId));
return Utils.responseContentType(convertOperationId(operationId));
},
generateParameterName: (operationId: string) => {
return Utils.parameterName(convertString(operationId));
return Utils.parameterName(convertOperationId(operationId));
},
generateRequestBodyName: (operationId: string) => {
return Utils.requestBodyName(convertString(operationId));
return Utils.requestBodyName(convertOperationId(operationId));
},
generateFunctionName: (operationId: string) => {
return convertString(operationId);
return convertOperationId(operationId);
},
convertFormatTypeNode: schema => {
const formatConversions = options?.formatConversions;
Expand Down
8 changes: 6 additions & 2 deletions src/internal/OpenApiTools/toTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,12 @@ export const convert: Convert = (
Logger.info("Parent Schema:");
Logger.info(JSON.stringify(option.parent));
}
Logger.showFilePosition(entryPoint, currentPoint);
throw new UnsetTypeError("Please set 'type' or '$ref' property \n" + JSON.stringify(schema));
const typeNode = factory.TypeNode.create({
type: "any",
});
return typeNode;
// Logger.showFilePosition(entryPoint, currentPoint);
// throw new UnsetTypeError("Please set 'type' or '$ref' property \n" + JSON.stringify(schema));
}
switch (schema.type) {
case "boolean": {
Expand Down
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ export const isAvailableVariableName = (text: string): boolean => {
return /^[A-Za-z_0-9\s]+$/.test(text);
};

export const isFirstCharacterIsValidText = (text: string): boolean => {
return /^[A-Za-z]+/.test(text);
}

export const escapeText = (text: string): string => {
if (isAvailableVariableName(text)) {
if (isAvailableVariableName(text) && isFirstCharacterIsValidText(text)) {
return text;
}
return `"${text}"`;
Expand Down
86 changes: 86 additions & 0 deletions test/__tests__/__snapshots__/parameter-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,92 @@ exports[`Parameter api.test.domain 1`] = `
}
}
},
{
\\"operationId\\": \\"i.have.dot\\",
\\"convertedParams\\": {
\\"escapedOperationId\\": \\"i$have$dot\\",
\\"argumentParamsTypeDeclaration\\": \\"Params$i$have$dot\\",
\\"functionName\\": \\"i$have$dot\\",
\\"requestContentTypeName\\": \\"RequestContentType$i$have$dot\\",
\\"responseContentTypeName\\": \\"ResponseContentType$i$have$dot\\",
\\"parameterName\\": \\"Parameter$i$have$dot\\",
\\"requestBodyName\\": \\"RequestBody$i$have$dot\\",
\\"hasRequestBody\\": false,
\\"hasParameter\\": false,
\\"pickedParameters\\": [],
\\"requestContentTypes\\": [],
\\"responseSuccessNames\\": [
\\"Response$i$have$dot$Status$200\\"
],
\\"responseFirstSuccessName\\": \\"Response$i$have$dot$Status$200\\",
\\"has2OrMoreSuccessNames\\": false,
\\"responseErrorNames\\": [],
\\"has2OrMoreRequestContentTypes\\": false,
\\"successResponseContentTypes\\": [
\\"application/json\\"
],
\\"successResponseFirstContentType\\": \\"application/json\\",
\\"has2OrMoreSuccessResponseContentTypes\\": false,
\\"hasAdditionalHeaders\\": false,
\\"hasQueryParameters\\": false
},
\\"operationParams\\": {
\\"httpMethod\\": \\"get\\",
\\"requestUri\\": \\"/i/have/dot\\",
\\"comment\\": \\"\\",
\\"deprecated\\": false,
\\"parameters\\": [],
\\"responses\\": {
\\"200\\": {
\\"description\\": \\"Get Books\\",
\\"content\\": {
\\"application/json\\": {
\\"schema\\": {
\\"type\\": \\"object\\",
\\"properties\\": {
\\"books\\": {
\\"type\\": \\"array\\",
\\"items\\": {
\\"type\\": \\"object\\",
\\"description\\": \\"Item Model\\",
\\"required\\": [
\\"name\\",
\\"children\\"
],
\\"properties\\": {
\\"name\\": {
\\"type\\": \\"string\\"
},
\\"children\\": {
\\"type\\": \\"array\\",
\\"items\\": {
\\"type\\": \\"object\\",
\\"required\\": [
\\"id\\",
\\"name\\"
],
\\"properties\\": {
\\"id\\": {
\\"type\\": \\"string\\"
},
\\"name\\": {
\\"type\\": \\"string\\",
\\"description\\": \\"child name\\"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
},
{
\\"operationId\\": \\"getReferenceItems\\",
\\"convertedParams\\": {
Expand Down
20 changes: 19 additions & 1 deletion test/__tests__/__snapshots__/spit-code-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ export interface Response$getFullRemoteReference$Status$200 {
name?: \\"responseA\\";
};
}
export interface Response$i$have$dot$Status$200 {
\\"application/json\\": {
books?: Schemas.Item[];
};
}
export interface Response$getReferenceItems$Status$200 {
\\"application/json\\": {
books?: Schemas.Item[];
Expand Down Expand Up @@ -81,6 +86,7 @@ export type ResponseContentType$getFullRemoteReference = keyof Response$getFullR
export interface Params$getFullRemoteReference {
parameter: Parameter$getFullRemoteReference;
}
export type ResponseContentType$i$have$dot = keyof Response$i$have$dot$Status$200;
export type ResponseContentType$getReferenceItems = keyof Response$getReferenceItems$Status$200;
export type ResponseContentType$searchBook = keyof Response$searchBook$Status$200;
export interface Params$searchBook {
Expand All @@ -106,11 +112,12 @@ export interface QueryParameter {
export interface QueryParameters {
[key: string]: QueryParameter;
}
export type SuccessResponses = Response$getIncludeLocalReference$Status$200 | Response$getFullRemoteReference$Status$200 | Response$getReferenceItems$Status$200 | Response$searchBook$Status$200 | Response$getBookById$Status$200 | Response$deleteBook$Status$200;
export type SuccessResponses = Response$getIncludeLocalReference$Status$200 | Response$getFullRemoteReference$Status$200 | Response$i$have$dot$Status$200 | Response$getReferenceItems$Status$200 | Response$searchBook$Status$200 | Response$getBookById$Status$200 | Response$deleteBook$Status$200;
export namespace ErrorResponse {
export type getIncludeLocalReference = void;
export type getIncludeRemoteReference = void;
export type getFullRemoteReference = void;
export type i$have$dot = void;
export type getReferenceItems = void;
export type searchBook = void;
export type getBookById = void;
Expand Down Expand Up @@ -164,6 +171,17 @@ export class Client<RequestOption> {
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
}
/**
* operationId: i.have.dot
* Request URI: /i/have/dot
*/
public async i$have$dot(option?: RequestOption): Promise<Response$i$have$dot$Status$200[\\"application/json\\"]> {
const url = this.baseUrl + \`/i/have/dot\`;
const headers = {
Accept: \\"application/json\\"
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
}
/**
* operationId: getReferenceItems
* Request URI: /get/reference/items
Expand Down
22 changes: 20 additions & 2 deletions test/__tests__/__snapshots__/template-only-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type ResponseContentType$getFullRemoteReference = keyof Response$getFullR
export interface Params$getFullRemoteReference {
parameter: Parameter$getFullRemoteReference;
}
export type ResponseContentType$i$have$dot = keyof Response$i$have$dot$Status$200;
export type ResponseContentType$getReferenceItems = keyof Response$getReferenceItems$Status$200;
export type ResponseContentType$searchBook = keyof Response$searchBook$Status$200;
export interface Params$searchBook {
Expand All @@ -48,11 +49,12 @@ export interface QueryParameter {
export interface QueryParameters {
[key: string]: QueryParameter;
}
export type SuccessResponses = Response$getIncludeLocalReference$Status$200 | Response$getFullRemoteReference$Status$200 | Response$getReferenceItems$Status$200 | Response$searchBook$Status$200 | Response$getBookById$Status$200 | Response$deleteBook$Status$200;
export type SuccessResponses = Response$getIncludeLocalReference$Status$200 | Response$getFullRemoteReference$Status$200 | Response$i$have$dot$Status$200 | Response$getReferenceItems$Status$200 | Response$searchBook$Status$200 | Response$getBookById$Status$200 | Response$deleteBook$Status$200;
export namespace ErrorResponse {
export type getIncludeLocalReference = void;
export type getIncludeRemoteReference = void;
export type getFullRemoteReference = void;
export type i$have$dot = void;
export type getReferenceItems = void;
export type searchBook = void;
export type getBookById = void;
Expand Down Expand Up @@ -94,6 +96,13 @@ export class Client<RequestOption> {
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
}
public async i$have$dot(option?: RequestOption): Promise<Response$i$have$dot$Status$200[\\"application/json\\"]> {
const url = this.baseUrl + \`/i/have/dot\`;
const headers = {
Accept: \\"application/json\\"
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
}
public async getReferenceItems(option?: RequestOption): Promise<Response$getReferenceItems$Status$200[\\"application/json\\"]> {
const url = this.baseUrl + \`/get/reference/items\`;
const headers = {
Expand Down Expand Up @@ -152,6 +161,7 @@ export type ResponseContentType$getFullRemoteReference = keyof Response$getFullR
export interface Params$getFullRemoteReference {
parameter: Parameter$getFullRemoteReference;
}
export type ResponseContentType$i$have$dot = keyof Response$i$have$dot$Status$200;
export type ResponseContentType$getReferenceItems = keyof Response$getReferenceItems$Status$200;
export type ResponseContentType$searchBook = keyof Response$searchBook$Status$200;
export interface Params$searchBook {
Expand All @@ -177,11 +187,12 @@ export interface QueryParameter {
export interface QueryParameters {
[key: string]: QueryParameter;
}
export type SuccessResponses = Response$getIncludeLocalReference$Status$200 | Response$getFullRemoteReference$Status$200 | Response$getReferenceItems$Status$200 | Response$searchBook$Status$200 | Response$getBookById$Status$200 | Response$deleteBook$Status$200;
export type SuccessResponses = Response$getIncludeLocalReference$Status$200 | Response$getFullRemoteReference$Status$200 | Response$i$have$dot$Status$200 | Response$getReferenceItems$Status$200 | Response$searchBook$Status$200 | Response$getBookById$Status$200 | Response$deleteBook$Status$200;
export namespace ErrorResponse {
export type getIncludeLocalReference = void;
export type getIncludeRemoteReference = void;
export type getFullRemoteReference = void;
export type i$have$dot = void;
export type getReferenceItems = void;
export type searchBook = void;
export type getBookById = void;
Expand Down Expand Up @@ -223,6 +234,13 @@ export class Client<RequestOption> {
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
}
public i$have$dot(option?: RequestOption): Response$i$have$dot$Status$200[\\"application/json\\"] {
const url = this.baseUrl + \`/i/have/dot\`;
const headers = {
Accept: \\"application/json\\"
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
}
public getReferenceItems(option?: RequestOption): Response$getReferenceItems$Status$200[\\"application/json\\"] {
const url = this.baseUrl + \`/get/reference/items\`;
const headers = {
Expand Down
32 changes: 30 additions & 2 deletions test/__tests__/__snapshots__/typedef-with-template-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ export interface Response$getFullRemoteReference$Status$200 {
name?: \\"responseA\\";
};
}
export interface Response$i$have$dot$Status$200 {
\\"application/json\\": {
books?: Schemas.Item[];
};
}
export interface Response$getReferenceItems$Status$200 {
\\"application/json\\": {
books?: Schemas.Item[];
Expand Down Expand Up @@ -357,6 +362,7 @@ export type ResponseContentType$getFullRemoteReference = keyof Response$getFullR
export interface Params$getFullRemoteReference {
parameter: Parameter$getFullRemoteReference;
}
export type ResponseContentType$i$have$dot = keyof Response$i$have$dot$Status$200;
export type ResponseContentType$getReferenceItems = keyof Response$getReferenceItems$Status$200;
export type ResponseContentType$searchBook = keyof Response$searchBook$Status$200;
export interface Params$searchBook {
Expand All @@ -382,11 +388,12 @@ export interface QueryParameter {
export interface QueryParameters {
[key: string]: QueryParameter;
}
export type SuccessResponses = Response$getIncludeLocalReference$Status$200 | Response$getFullRemoteReference$Status$200 | Response$getReferenceItems$Status$200 | Response$searchBook$Status$200 | Response$getBookById$Status$200 | Response$deleteBook$Status$200;
export type SuccessResponses = Response$getIncludeLocalReference$Status$200 | Response$getFullRemoteReference$Status$200 | Response$i$have$dot$Status$200 | Response$getReferenceItems$Status$200 | Response$searchBook$Status$200 | Response$getBookById$Status$200 | Response$deleteBook$Status$200;
export namespace ErrorResponse {
export type getIncludeLocalReference = void;
export type getIncludeRemoteReference = void;
export type getFullRemoteReference = void;
export type i$have$dot = void;
export type getReferenceItems = void;
export type searchBook = void;
export type getBookById = void;
Expand Down Expand Up @@ -428,6 +435,13 @@ export class Client<RequestOption> {
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
}
public async i$have$dot(option?: RequestOption): Promise<Response$i$have$dot$Status$200[\\"application/json\\"]> {
const url = this.baseUrl + \`/i/have/dot\`;
const headers = {
Accept: \\"application/json\\"
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
}
public async getReferenceItems(option?: RequestOption): Promise<Response$getReferenceItems$Status$200[\\"application/json\\"]> {
const url = this.baseUrl + \`/get/reference/items\`;
const headers = {
Expand Down Expand Up @@ -839,6 +853,11 @@ export interface Response$getFullRemoteReference$Status$200 {
name?: \\"responseA\\";
};
}
export interface Response$i$have$dot$Status$200 {
\\"application/json\\": {
books?: Schemas.Item[];
};
}
export interface Response$getReferenceItems$Status$200 {
\\"application/json\\": {
books?: Schemas.Item[];
Expand Down Expand Up @@ -883,6 +902,7 @@ export type ResponseContentType$getFullRemoteReference = keyof Response$getFullR
export interface Params$getFullRemoteReference {
parameter: Parameter$getFullRemoteReference;
}
export type ResponseContentType$i$have$dot = keyof Response$i$have$dot$Status$200;
export type ResponseContentType$getReferenceItems = keyof Response$getReferenceItems$Status$200;
export type ResponseContentType$searchBook = keyof Response$searchBook$Status$200;
export interface Params$searchBook {
Expand All @@ -908,11 +928,12 @@ export interface QueryParameter {
export interface QueryParameters {
[key: string]: QueryParameter;
}
export type SuccessResponses = Response$getIncludeLocalReference$Status$200 | Response$getFullRemoteReference$Status$200 | Response$getReferenceItems$Status$200 | Response$searchBook$Status$200 | Response$getBookById$Status$200 | Response$deleteBook$Status$200;
export type SuccessResponses = Response$getIncludeLocalReference$Status$200 | Response$getFullRemoteReference$Status$200 | Response$i$have$dot$Status$200 | Response$getReferenceItems$Status$200 | Response$searchBook$Status$200 | Response$getBookById$Status$200 | Response$deleteBook$Status$200;
export namespace ErrorResponse {
export type getIncludeLocalReference = void;
export type getIncludeRemoteReference = void;
export type getFullRemoteReference = void;
export type i$have$dot = void;
export type getReferenceItems = void;
export type searchBook = void;
export type getBookById = void;
Expand Down Expand Up @@ -954,6 +975,13 @@ export class Client<RequestOption> {
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, queryParameters, option);
}
public i$have$dot(option?: RequestOption): Response$i$have$dot$Status$200[\\"application/json\\"] {
const url = this.baseUrl + \`/i/have/dot\`;
const headers = {
Accept: \\"application/json\\"
};
return this.apiClient.request(\\"GET\\", url, headers, undefined, undefined, option);
}
public getReferenceItems(option?: RequestOption): Response$getReferenceItems$Status$200[\\"application/json\\"] {
const url = this.baseUrl + \`/get/reference/items\`;
const headers = {
Expand Down
Loading

0 comments on commit b876a7c

Please sign in to comment.