Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Typescript][Fetch] Fix bugs with new typescript-fetch generator #1545

Merged
merged 6 commits into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.CodegenConstants.MODEL_PROPERTY_NAMING_TYPE;
Expand Down Expand Up @@ -132,9 +134,12 @@ public String getTypeDeclaration(Schema p) {
return "Blob";
} else if (ModelUtils.isBinarySchema(p)) {
return "Blob";
} else {
return super.getTypeDeclaration(p);
} else if (ModelUtils.isDateSchema(p)) {
return "Date";
} else if (ModelUtils.isDateTimeSchema(p)) {
return "Date";
}
return super.getTypeDeclaration(p);
}

@Override
Expand Down Expand Up @@ -183,6 +188,7 @@ private void addNpmPackageGeneration() {
@Override
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> operations, List<Object> allModels) {
this.addOperationModelImportInfomation(operations);
this.updateOperationParameterEnumInformation(operations);
return operations;
}

Expand All @@ -196,6 +202,25 @@ private void addOperationModelImportInfomation(Map<String, Object> operations) {
}
}

private void updateOperationParameterEnumInformation(Map<String, Object> operations) {
// This method will add extra infomation as to whether or not we have enums and
// update their names with the operation.id prefixed.
Map<String, Object> _operations = (Map<String, Object>) operations.get("operations");
List<CodegenOperation> operationList = (List<CodegenOperation>) _operations.get("operation");
boolean hasEnum = false;
for (CodegenOperation op : operationList) {
for (CodegenParameter param : op.allParams) {
if (Boolean.TRUE.equals(param.isEnum)) {
hasEnum = true;
param.datatypeWithEnum = param.datatypeWithEnum
.replace(param.enumName, op.operationIdCamelCase + param.enumName);
}
}
}

operations.put("hasEnums", hasEnum);
}

private void addExtraReservedWords() {
this.reservedWords.add("BASE_PATH");
this.reservedWords.add("BaseAPI");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
{{#allParams.0}}
export interface {{operationIdCamelCase}}Request {
{{#allParams}}
{{paramName}}{{^required}}?{{/required}}: {{{dataType}}};
{{paramName}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}};
{{/allParams}}
}

Expand Down Expand Up @@ -48,10 +48,8 @@ export class {{classname}} extends runtime.BaseAPI {

{{/required}}
{{/allParams}}
{{#hasQueryParams}}
const queryParameters: runtime.HTTPQuery = {};

{{/hasQueryParams}}
{{#queryParams}}
{{#isListContainer}}
if (requestParameters.{{paramName}}) {
Expand Down Expand Up @@ -168,9 +166,7 @@ export class {{classname}} extends runtime.BaseAPI {
path: `{{{path}}}`{{#pathParams}}.replace(`{${"{{baseName}}"}}`, encodeURIComponent(String(requestParameters.{{paramName}}))){{/pathParams}},
method: '{{httpMethod}}',
headers: headerParameters,
{{#hasQueryParams}}
query: queryParameters,
{{/hasQueryParams}}
{{#hasBodyParam}}
{{#bodyParam}}
{{#isContainer}}
Expand Down Expand Up @@ -243,3 +239,25 @@ export class {{classname}} extends runtime.BaseAPI {
{{/operation}}
}
{{/operations}}
{{#hasEnums}}

{{#operations}}
{{#operation}}
{{#allParams}}
{{#isEnum}}
/**
* @export
* @enum {string}
*/
export enum {{operationIdCamelCase}}{{enumName}} {
{{#allowableValues}}
{{#enumVars}}
{{{name}}} = {{{value}}}{{^-last}},{{/-last}}
{{/enumVars}}
{{/allowableValues}}
}
{{/isEnum}}
{{/allParams}}
{{/operation}}
{{/operations}}
{{/hasEnums}}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
* @type {{=<% %>=}}{<%&datatype%>}<%={{ }}=%>
* @memberof {{classname}}
*/
{{name}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}};
{{#isReadOnly}}readonly {{/isReadOnly}}{{name}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}};
{{/vars}}
}

export function {{classname}}FromJSON(json: any): {{classname}} {
{{#hasVars}}
return {
{{#vars}}
{{#allVars}}
{{#isPrimitiveType}}
{{#isDate}}
'{{name}}': {{^required}}!exists(json, '{{baseName}}') ? undefined : {{/required}}new Date(json['{{baseName}}']),
Expand All @@ -53,7 +53,7 @@ export function {{classname}}FromJSON(json: any): {{classname}} {
'{{name}}': {{^required}}!exists(json, '{{baseName}}') ? undefined : {{/required}}{{datatype}}FromJSON(json['{{baseName}}']),
{{/isContainer}}
{{/isPrimitiveType}}
{{/vars}}
{{/allVars}}
};
{{/hasVars}}
{{^hasVars}}
Expand All @@ -67,9 +67,10 @@ export function {{classname}}ToJSON(value?: {{classname}}): any {
return undefined;
}
return {
{{#vars}}
{{#allVars}}
{{^isReadOnly}}
{{#isPrimitiveType}}
'{{baseName}}': {{#isDate}}{{^required}}value.{{name}} === undefined ? undefined : {{/required}}value.{{name}}.toISOString(){{/isDate}}{{#isDateTime}}{{^required}}value.{{name}} === undefined ? undefined : {{/required}}value.{{name}}.toISOString(){{/isDateTime}}{{^isDate}}{{^isDateTime}}value.{{name}}{{/isDateTime}}{{/isDate}},
'{{baseName}}': {{#isDate}}{{^required}}value.{{name}} === undefined ? undefined : {{/required}}value.{{name}}.toISOString().substr(0,10){{/isDate}}{{#isDateTime}}{{^required}}value.{{name}} === undefined ? undefined : {{/required}}value.{{name}}.toISOString(){{/isDateTime}}{{^isDate}}{{^isDateTime}}value.{{name}}{{/isDateTime}}{{/isDate}},
{{/isPrimitiveType}}
{{^isPrimitiveType}}
{{#isContainer}}
Expand All @@ -79,7 +80,8 @@ export function {{classname}}ToJSON(value?: {{classname}}): any {
'{{baseName}}': {{datatype}}ToJSON(value.{{name}}),
{{/isContainer}}
{{/isPrimitiveType}}
{{/vars}}
{{/isReadOnly}}
{{/allVars}}
};
{{/hasVars}}
{{^hasVars}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"declaration": true,
"target": "{{#supportsES6}}es6{{/supportsES6}}{{^supportsES6}}es5{{/supportsES6}}",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "dist",
"rootDir": "."{{^supportsES6}},
"lib": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void simpleModelTest() {
final CodegenProperty property4 = cm.vars.get(3);
Assert.assertEquals(property4.baseName, "birthDate");
Assert.assertEquals(property4.complexType, null);
Assert.assertEquals(property4.dataType, "string");
Assert.assertEquals(property4.dataType, "Date");
Assert.assertEquals(property4.name, "birthDate");
Assert.assertEquals(property4.defaultValue, "undefined");
Assert.assertFalse(property4.hasMore);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.2-SNAPSHOT
4.0.0-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// tslint:disable
/**
* OpenAPI Petstore *_/ ' \" =end -- \\r\\n \\n \\r
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
*
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
* Contact: something@something.abc *_/ ' \" =end -- \\r\\n \\n \\r
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/


import * as runtime from '../runtime';
import {
UNKNOWN_BASE_TYPE,
UNKNOWN_BASE_TYPEFromJSON,
UNKNOWN_BASE_TYPEToJSON,
} from '../models';

export interface TestCodeInjectEndRnNRRequest {
UNKNOWN_BASE_TYPE?: UNKNOWN_BASE_TYPE;
}

/**
* no description
*/
export class FakeApi extends runtime.BaseAPI {

/**
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
*/
async testCodeInjectEndRnNRRaw(requestParameters: TestCodeInjectEndRnNRRequest): Promise<runtime.ApiResponse<void>> {
const queryParameters: runtime.HTTPQuery = {};

const headerParameters: runtime.HTTPHeaders = {};

headerParameters['Content-Type'] = 'application/json';

const response = await this.request({
path: `/fake`,
method: 'PUT',
headers: headerParameters,
query: queryParameters,
body: UNKNOWN_BASE_TYPEToJSON(requestParameters.UNKNOWN_BASE_TYPE),
});

return new runtime.VoidApiResponse(response);
}

/**
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
*/
async testCodeInjectEndRnNR(requestParameters: TestCodeInjectEndRnNRRequest): Promise<void> {
await this.testCodeInjectEndRnNRRaw(requestParameters);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './FakeApi';
19 changes: 3 additions & 16 deletions samples/client/petstore-security-test/typescript-fetch/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
// tslint:disable
/**
* OpenAPI Petstore *_/ ' \" =end -- \\r\\n \\n \\r
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
*
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
* Contact: something@something.abc *_/ ' \" =end -- \\r\\n \\n \\r
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/


export * from "./api";
export * from "./configuration";
export * from './runtime';
export * from './apis';
export * from './models';
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// tslint:disable
/**
* OpenAPI Petstore *_/ ' \" =end -- \\r\\n \\n \\r
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
*
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
* Contact: something@something.abc *_/ ' \" =end -- \\r\\n \\n \\r
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

import { exists } from '../runtime';
/**
* Model for testing reserved words *_/ ' \" =end -- \\r\\n \\n \\r
* @export
* @interface Return
*/
export interface Return {
/**
* property description *_/ ' \" =end -- \\r\\n \\n \\r
* @type {number}
* @memberof Return
*/
_return?: number;
}

export function ReturnFromJSON(json: any): Return {
return {
'_return': !exists(json, 'return') ? undefined : json['return'],
};
}

export function ReturnToJSON(value?: Return): any {
if (value === undefined) {
return undefined;
}
return {
'return': value._return,
};
}


Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './Return';
Loading