From f8046bc9c9bc078eea34d8beb6b81e01535e27c4 Mon Sep 17 00:00:00 2001 From: Kristof Vrolijkx Date: Sun, 1 May 2016 14:27:41 +0200 Subject: [PATCH 01/10] small example of a first integration-test of the code-generator --- .../java/io/swagger/codegen/ClientOpts.java | 54 ++------- ...r2GenerationWithAditionPropertiesTest.java | 74 ++++++++++++ .../integrationtest/AssertFile.java | 94 +++++++++++++++ .../TypeScriptAngularClientOptionsTest.java | 2 +- .../TypeScriptAngularModelTest.java | 2 +- .../TypeScriptAngular2ClientOptionsTest.java | 2 +- .../TypeScriptAngular2ModelTest.java | 2 +- .../TypeScriptNodeClientOptionsTest.java | 2 +- .../TypeScriptNodeModelTest.java | 2 +- .../additional-properties-expected/README.md | 33 ++++++ .../api/UserApi.ts | 64 ++++++++++ .../additional-properties-expected/api/api.ts | 3 + .../additional-properties-expected/index.ts | 2 + .../model/User.ts | 14 +++ .../model/models.ts | 4 + .../package.json | 30 +++++ .../tsconfig.json | 27 +++++ .../typings.json | 5 + .../additional-properties-spec.json | 110 ++++++++++++++++++ 19 files changed, 474 insertions(+), 52 deletions(-) create mode 100644 modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/Integrationtest/Angular2GenerationWithAditionPropertiesTest.java create mode 100644 modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/integrationtest/AssertFile.java rename modules/swagger-codegen/src/test/java/io/swagger/codegen/{ => typescript}/typescriptangular/TypeScriptAngularClientOptionsTest.java (95%) rename modules/swagger-codegen/src/test/java/io/swagger/codegen/{ => typescript}/typescriptangular/TypeScriptAngularModelTest.java (99%) rename modules/swagger-codegen/src/test/java/io/swagger/codegen/{ => typescript}/typescriptangular2/TypeScriptAngular2ClientOptionsTest.java (95%) rename modules/swagger-codegen/src/test/java/io/swagger/codegen/{ => typescript}/typescriptangular2/TypeScriptAngular2ModelTest.java (99%) rename modules/swagger-codegen/src/test/java/io/swagger/codegen/{ => typescript}/typescriptnode/TypeScriptNodeClientOptionsTest.java (95%) rename modules/swagger-codegen/src/test/java/io/swagger/codegen/{ => typescript}/typescriptnode/TypeScriptNodeModelTest.java (99%) create mode 100644 modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/README.md create mode 100644 modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/api/UserApi.ts create mode 100644 modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/api/api.ts create mode 100644 modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/index.ts create mode 100644 modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/model/User.ts create mode 100644 modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/model/models.ts create mode 100644 modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/package.json create mode 100644 modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/tsconfig.json create mode 100644 modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/typings.json create mode 100644 modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-spec.json diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/ClientOpts.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/ClientOpts.java index 1087de5786d..8de7476c5e2 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/ClientOpts.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/ClientOpts.java @@ -1,32 +1,13 @@ package io.swagger.codegen; -import io.swagger.codegen.auth.AuthMethod; - import java.util.HashMap; import java.util.Map; +import io.swagger.codegen.auth.AuthMethod; + public class ClientOpts { - protected String uri; - protected String target; protected AuthMethod auth; protected Map properties = new HashMap(); - protected String outputDirectory; - - public String getUri() { - return uri; - } - - public void setUri(String uri) { - this.uri = uri; - } - - public String getTarget() { - return target; - } - - public void setTarget(String target) { - this.target = target; - } public Map getProperties() { return properties; @@ -36,19 +17,10 @@ public class ClientOpts { this.properties = properties; } - public String getOutputDirectory() { - return outputDirectory; - } - - public void setOutputDirectory(String outputDirectory) { - this.outputDirectory = outputDirectory; - } - @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("ClientOpts: {\n"); - sb.append(" uri: ").append(uri).append(","); sb.append(" auth: ").append(auth).append(","); sb.append(properties); sb.append("}"); @@ -57,30 +29,20 @@ public class ClientOpts { @Override public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { return true; } + if (o == null || getClass() != o.getClass()) { return false; } ClientOpts that = (ClientOpts) o; - if (uri != null ? !uri.equals(that.uri) : that.uri != null) - return false; - if (target != null ? !target.equals(that.target) : that.target != null) - return false; - if (auth != null ? !auth.equals(that.auth) : that.auth != null) - return false; - if (properties != null ? !properties.equals(that.properties) : that.properties != null) - return false; - return outputDirectory != null ? outputDirectory.equals(that.outputDirectory) : that.outputDirectory == null; + if (auth != null ? !auth.equals(that.auth) : that.auth != null) { return false; } + return getProperties().equals(that.getProperties()); } @Override public int hashCode() { - int result = uri != null ? uri.hashCode() : 0; - result = 31 * result + (target != null ? target.hashCode() : 0); - result = 31 * result + (auth != null ? auth.hashCode() : 0); - result = 31 * result + (properties != null ? properties.hashCode() : 0); - result = 31 * result + (outputDirectory != null ? outputDirectory.hashCode() : 0); + int result = auth != null ? auth.hashCode() : 0; + result = 31 * result + getProperties().hashCode(); return result; } } diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/Integrationtest/Angular2GenerationWithAditionPropertiesTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/Integrationtest/Angular2GenerationWithAditionPropertiesTest.java new file mode 100644 index 00000000000..98878490fa6 --- /dev/null +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/Integrationtest/Angular2GenerationWithAditionPropertiesTest.java @@ -0,0 +1,74 @@ +package io.swagger.codegen.typescript.integrationtest; + +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; +import org.testng.reporters.Files; + +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; + +import io.swagger.codegen.ClientOptInput; +import io.swagger.codegen.ClientOpts; +import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.DefaultGenerator; +import io.swagger.codegen.languages.TypeScriptAngular2ClientCodegen; +import io.swagger.models.Swagger; +import io.swagger.parser.SwaggerParser; + +import static io.swagger.codegen.typescript.integrationtest.AssertFile.assertPathEqualsRecursively; + +public class Angular2GenerationWithAditionPropertiesTest { + + private DefaultGenerator codeGen; + private Path integrationTestPath; + private Path outputPath; + private Path specPath; + private Path expectedPath; + + @BeforeMethod + public void setUp() { + codeGen = new DefaultGenerator(); + integrationTestPath = Paths.get("target/test-classes/integrationtest").toAbsolutePath(); + outputPath = integrationTestPath.resolve("typescript/additional-properties-result"); + expectedPath = integrationTestPath.resolve("typescript/additional-properties-expected"); + specPath = integrationTestPath.resolve("typescript/additional-properties-spec.json"); + + } + + protected CodegenConfig getCodegenConfig() { + return new TypeScriptAngular2ClientCodegen(); + } + + protected Map configProperties() { + Map propeties = new HashMap<>(); + propeties.put("npmName", "additionalPropertiesTest"); + propeties.put("npmVersion", "1.0.2"); + propeties.put("snapshot", "false"); + + return propeties; + } + + @Test(description = "The correct output is generated for a spec with additional-properties") + public void shouldGenerateCorrectTypescriptModels() throws IOException { + String specContent = Files.readFile(specPath.toFile()); + Swagger swagger = new SwaggerParser().parse(specContent); + + CodegenConfig codegenConfig = getCodegenConfig(); + codegenConfig.setOutputDir(outputPath.toString()); + + ClientOpts clientOpts = new ClientOpts(); + clientOpts.setProperties(configProperties()); + ClientOptInput opts = new ClientOptInput() + .config(codegenConfig) + .opts(clientOpts) + .swagger(swagger); + + codeGen.opts(opts).generate(); + + assertPathEqualsRecursively(expectedPath, outputPath); + } + +} diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/integrationtest/AssertFile.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/integrationtest/AssertFile.java new file mode 100644 index 00000000000..6f78c80a755 --- /dev/null +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/integrationtest/AssertFile.java @@ -0,0 +1,94 @@ +package io.swagger.codegen.typescript.integrationtest; + +import org.testng.Assert; + +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.FileVisitResult; +import java.nio.file.FileVisitor; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributes; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.fail; + +/** + * Assertion for recursively testing directories. + * + * @author andreas + */ +public class AssertFile { + + private AssertFile() { + throw new RuntimeException("This class should not be instantiated"); + } + + /** + * Asserts that two directories are recursively equal. If they are not, an {@link AssertionError} is thrown with the + * given message.
+ * There will be a binary comparison of all files under expected with all files under actual. File attributes will + * not be considered.
+ * Missing or additional files are considered an error.
+ * + * @param expected Path expected directory + * @param actual Path actual directory + */ + public static final void assertPathEqualsRecursively(final Path expected, final Path actual) { + Assert.assertNotNull(expected); + Assert.assertNotNull(actual); + final Path absoluteExpected = expected.toAbsolutePath(); + final Path absoluteActual = actual.toAbsolutePath(); + try { + Files.walkFileTree(expected, new FileVisitor() { + + @Override + public FileVisitResult preVisitDirectory(Path expectedDir, BasicFileAttributes attrs) throws IOException { + Path relativeExpectedDir = absoluteExpected.relativize(expectedDir.toAbsolutePath()); + Path actualDir = absoluteActual.resolve(relativeExpectedDir); + + if (!Files.exists(actualDir)) { + fail(String.format("Directory \'%s\' missing in target.", expectedDir.getFileName())); + } + + assertEquals(expectedDir.toFile().list().length, + actualDir.toFile().list().length, + String.format("Directory size of \'%s\' differ. ", relativeExpectedDir)); + + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path expectedFile, BasicFileAttributes attrs) throws IOException { + Path relativeExpectedFile = absoluteExpected.relativize(expectedFile.toAbsolutePath()); + Path actualFile = absoluteActual.resolve(relativeExpectedFile); + + if (!Files.exists(actualFile)) { + fail(String.format("File \'%s\' missing in target.", expectedFile.getFileName())); + } + assertEquals(Files.readAllLines(expectedFile, Charset.defaultCharset()), + Files.readAllLines(actualFile, Charset.defaultCharset()), + String.format("File content of \'%s\' differ. ", relativeExpectedFile)); + + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException { + fail(exc.getMessage()); + return FileVisitResult.TERMINATE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + return FileVisitResult.CONTINUE; + } + + }); + } catch (IOException e) { + fail(e.getMessage()); + } + } + +} + diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptangular/TypeScriptAngularClientOptionsTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular/TypeScriptAngularClientOptionsTest.java similarity index 95% rename from modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptangular/TypeScriptAngularClientOptionsTest.java rename to modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular/TypeScriptAngularClientOptionsTest.java index 17d9c1ed205..a4f5759fe3f 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptangular/TypeScriptAngularClientOptionsTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular/TypeScriptAngularClientOptionsTest.java @@ -1,4 +1,4 @@ -package io.swagger.codegen.typescriptangular; +package io.swagger.codegen.typescript.typescriptangular; import io.swagger.codegen.AbstractOptionsTest; import io.swagger.codegen.CodegenConfig; diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptangular/TypeScriptAngularModelTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular/TypeScriptAngularModelTest.java similarity index 99% rename from modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptangular/TypeScriptAngularModelTest.java rename to modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular/TypeScriptAngularModelTest.java index 26e8f841be9..75ab210966d 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptangular/TypeScriptAngularModelTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular/TypeScriptAngularModelTest.java @@ -1,4 +1,4 @@ -package io.swagger.codegen.typescriptangular; +package io.swagger.codegen.typescript.typescriptangular; import io.swagger.codegen.CodegenModel; import io.swagger.codegen.CodegenProperty; diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptangular2/TypeScriptAngular2ClientOptionsTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypeScriptAngular2ClientOptionsTest.java similarity index 95% rename from modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptangular2/TypeScriptAngular2ClientOptionsTest.java rename to modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypeScriptAngular2ClientOptionsTest.java index 4c56a7dfab2..f2b7e561da4 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptangular2/TypeScriptAngular2ClientOptionsTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypeScriptAngular2ClientOptionsTest.java @@ -1,4 +1,4 @@ -package io.swagger.codegen.typescriptangular2; +package io.swagger.codegen.typescript.typescriptangular2; import io.swagger.codegen.AbstractOptionsTest; import io.swagger.codegen.CodegenConfig; diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptangular2/TypeScriptAngular2ModelTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypeScriptAngular2ModelTest.java similarity index 99% rename from modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptangular2/TypeScriptAngular2ModelTest.java rename to modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypeScriptAngular2ModelTest.java index 3aa33df7da4..52e291de676 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptangular2/TypeScriptAngular2ModelTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypeScriptAngular2ModelTest.java @@ -1,4 +1,4 @@ -package io.swagger.codegen.typescriptangular2; +package io.swagger.codegen.typescript.typescriptangular2; import com.google.common.collect.Sets; diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptnode/TypeScriptNodeClientOptionsTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptnode/TypeScriptNodeClientOptionsTest.java similarity index 95% rename from modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptnode/TypeScriptNodeClientOptionsTest.java rename to modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptnode/TypeScriptNodeClientOptionsTest.java index 67b55de138a..f22abe873f6 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptnode/TypeScriptNodeClientOptionsTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptnode/TypeScriptNodeClientOptionsTest.java @@ -1,4 +1,4 @@ -package io.swagger.codegen.typescriptnode; +package io.swagger.codegen.typescript.typescriptnode; import io.swagger.codegen.AbstractOptionsTest; import io.swagger.codegen.CodegenConfig; diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptnode/TypeScriptNodeModelTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptnode/TypeScriptNodeModelTest.java similarity index 99% rename from modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptnode/TypeScriptNodeModelTest.java rename to modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptnode/TypeScriptNodeModelTest.java index 81a67e87b87..37e4fb688e0 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptnode/TypeScriptNodeModelTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptnode/TypeScriptNodeModelTest.java @@ -1,4 +1,4 @@ -package io.swagger.codegen.typescriptnode; +package io.swagger.codegen.typescript.typescriptnode; import io.swagger.codegen.CodegenModel; import io.swagger.codegen.CodegenProperty; diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/README.md b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/README.md new file mode 100644 index 00000000000..92aa291c7aa --- /dev/null +++ b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/README.md @@ -0,0 +1,33 @@ +## additionalPropertiesTest@1.0.0 + +### Building + +To build an compile the typescript sources to javascript use: +``` +npm install +npm run build +``` + +### publishing + +First build the package than run ```npm publish``` + +### consuming + +navigate to the folder of your consuming project and run one of next commando's. + +_published:_ + +``` +npm install additionalPropertiesTest@1.0.0 --save +``` + +_unPublished (not recommended):_ + +``` +npm install PATH_TO_GENERATED_PACKAGE --save +``` + +In your angular2 project: + +TODO: paste example. diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/api/UserApi.ts b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/api/UserApi.ts new file mode 100644 index 00000000000..40f94703038 --- /dev/null +++ b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/api/UserApi.ts @@ -0,0 +1,64 @@ +import {Http, Headers, RequestOptionsArgs, Response, URLSearchParams} from 'angular2/http'; +import {Injectable} from 'angular2/core'; +import {Observable} from 'rxjs/Observable'; +import * as models from '../model/models'; + +/* tslint:disable:no-unused-variable member-ordering */ + +'use strict'; + +@Injectable() +export class UserApi { + protected basePath = 'http://additional-properties.swagger.io/v2'; + public defaultHeaders : Headers = new Headers(); + + constructor(protected http: Http, basePath: string) { + if (basePath) { + this.basePath = basePath; + } + } + + /** + * Add a new User to the store + * + * @param body User object that needs to be added to the store + */ + public addUser (body?: models.User, extraHttpRequestParams?: any ) : Observable<{}> { + const path = this.basePath + '/user'; + + let queryParameters: any = ""; // This should probably be an object in the future + let headerParams = this.defaultHeaders; + let requestOptions: RequestOptionsArgs = { + method: 'POST', + headers: headerParams, + search: queryParameters + }; + requestOptions.body = JSON.stringify(body); + + return this.http.request(path, requestOptions) + .map((response: Response) => response.json()); + } + + /** + * Update an existing User + * + * @param body User object that needs to be added to the store + */ + public updateUser (body?: models.User, extraHttpRequestParams?: any ) : Observable<{}> { + const path = this.basePath + '/user'; + + let queryParameters: any = ""; // This should probably be an object in the future + let headerParams = this.defaultHeaders; + let requestOptions: RequestOptionsArgs = { + method: 'PUT', + headers: headerParams, + search: queryParameters + }; + requestOptions.body = JSON.stringify(body); + + return this.http.request(path, requestOptions) + .map((response: Response) => response.json()); + } + +} + diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/api/api.ts b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/api/api.ts new file mode 100644 index 00000000000..5cae4dbb428 --- /dev/null +++ b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/api/api.ts @@ -0,0 +1,3 @@ +export * from '../api/UserApi'; + + diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/index.ts b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/index.ts new file mode 100644 index 00000000000..d6d02862e3c --- /dev/null +++ b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/index.ts @@ -0,0 +1,2 @@ +export * from 'api/api'; +export * from 'model/models'; \ No newline at end of file diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/model/User.ts b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/model/User.ts new file mode 100644 index 00000000000..b1ce0a0f144 --- /dev/null +++ b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/model/User.ts @@ -0,0 +1,14 @@ +'use strict'; +import * as models from './models'; + +export interface User { + [key: string]: string + + id?: number; + + /** + * User Status + */ + userStatus?: number; +} + diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/model/models.ts b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/model/models.ts new file mode 100644 index 00000000000..115b5f75ce4 --- /dev/null +++ b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/model/models.ts @@ -0,0 +1,4 @@ +export * from './User'; + + + diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/package.json b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/package.json new file mode 100644 index 00000000000..b75234c322a --- /dev/null +++ b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/package.json @@ -0,0 +1,30 @@ +{ + "name": "additionalPropertiesTest", + "version": "1.0.0", + "description": "swagger client for additionalPropertiesTest", + "author": "Swagger Codegen Contributors", + "keywords": [ + "swagger-client" + ], + "license": "MIT", + "files": [ + "lib" + ], + "main": "./lib/index.js", + "typings": "./lib/index.d.ts", + "scripts": { + "build": "typings install && tsc" + }, + "peerDependencies": { + "angular2": "^2.0.0-beta.15", + "rxjs": "^5.0.0-beta.2" + }, + "devDependencies": { + "typescript": "^1.8.10", + "typings": "^0.8.1", + "angular2": "^2.0.0-beta.15", + "es6-shim": "^0.35.0", + "es7-reflect-metadata": "^1.6.0", + "rxjs": "5.0.0-beta.2", + "zone.js": "^0.6.10" + }} diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/tsconfig.json b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/tsconfig.json new file mode 100644 index 00000000000..07fbdf7e1b1 --- /dev/null +++ b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "noImplicitAny": false, + "suppressImplicitAnyIndexErrors": true, + "target": "es5", + "module": "commonjs", + "moduleResolution": "node", + "removeComments": true, + "sourceMap": true, + "outDir": "./lib", + "noLib": false, + "declaration": true + }, + "exclude": [ + "node_modules", + "typings/main.d.ts", + "typings/main", + "lib" + ], + "filesGlob": [ + "./model/*.ts", + "./api/*.ts", + "typings/browser.d.ts" + ] +} diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/typings.json b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/typings.json new file mode 100644 index 00000000000..0848dcffe31 --- /dev/null +++ b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/typings.json @@ -0,0 +1,5 @@ +{ + "ambientDependencies": { + "core-js": "registry:dt/core-js#0.0.0+20160317120654" + } +} \ No newline at end of file diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-spec.json b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-spec.json new file mode 100644 index 00000000000..3a06b88986c --- /dev/null +++ b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-spec.json @@ -0,0 +1,110 @@ +{ + "swagger": "2.0", + "info": { + "description": "This is a test spec", + "version": "1.0.0", + "title": "Swagger Additional Properties", + "termsOfService": "http://swagger.io/terms/", + "contact": { + "email": "apiteam@swagger.io" + }, + "license": { + "name": "Apache 2.0", + "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + } + }, + "host": "additional-properties.swagger.io", + "basePath": "/v2", + "schemes": [ + "http" + ], + "paths": { + "/user": { + "post": { + "tags": [ + "user" + ], + "summary": "Add a new User to the store", + "description": "", + "operationId": "addUser", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "in": "body", + "name": "body", + "description": "User object that needs to be added to the store", + "required": false, + "schema": { + "$ref": "#/definitions/User" + } + } + ], + "responses": { + "405": { + "description": "Invalid input" + } + } + }, + "put": { + "tags": [ + "user" + ], + "summary": "Update an existing User", + "description": "", + "operationId": "updateUser", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "in": "body", + "name": "body", + "description": "User object that needs to be added to the store", + "required": false, + "schema": { + "$ref": "#/definitions/User" + } + } + ], + "responses": { + "405": { + "description": "Validation exception" + }, + "404": { + "description": "User not found" + }, + "400": { + "description": "Invalid ID supplied" + } + } + } + } + }, + "definitions": { + "User": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "userStatus": { + "type": "integer", + "format": "int32", + "description": "User Status" + } + }, + "additionalProperties": { + "type": "string" + } + } + } +} From 15feb208e7c8574b6cdf120451bbbb96509a863f Mon Sep 17 00:00:00 2001 From: Kristof Vrolijkx Date: Sun, 1 May 2016 17:06:12 +0200 Subject: [PATCH 02/10] improving integration test concept. --- .../codegen/AbstractIntegrationTest.java | 45 +++++++++++ ...r2GenerationWithAditionPropertiesTest.java | 74 ------------------- ...r2AdditionalPropertiesIntegrationTest.java | 31 ++++++++ .../integrationtest => utils}/AssertFile.java | 16 ++-- .../utils/IntegrationTestPathsConfig.java | 33 +++++++++ .../additional-properties-expected/api/api.ts | 3 - .../additional-properties-expected/README.md | 0 .../api/UserApi.ts | 0 .../additional-properties-expected/api/api.ts | 3 + .../additional-properties-expected/index.ts | 0 .../model/User.ts | 0 .../model/models.ts | 0 .../package.json | 0 .../tsconfig.json | 0 .../typings.json | 0 .../additional-properties-spec.json | 0 16 files changed, 120 insertions(+), 85 deletions(-) create mode 100644 modules/swagger-codegen/src/test/java/io/swagger/codegen/AbstractIntegrationTest.java delete mode 100644 modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/Integrationtest/Angular2GenerationWithAditionPropertiesTest.java create mode 100644 modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypescriptAngular2AdditionalPropertiesIntegrationTest.java rename modules/swagger-codegen/src/test/java/io/swagger/codegen/{typescript/integrationtest => utils}/AssertFile.java (80%) create mode 100644 modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/IntegrationTestPathsConfig.java delete mode 100644 modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/api/api.ts rename modules/swagger-codegen/src/test/resources/{integrationtest => integrationtests}/typescript/additional-properties-expected/README.md (100%) rename modules/swagger-codegen/src/test/resources/{integrationtest => integrationtests}/typescript/additional-properties-expected/api/UserApi.ts (100%) create mode 100644 modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/api.ts rename modules/swagger-codegen/src/test/resources/{integrationtest => integrationtests}/typescript/additional-properties-expected/index.ts (100%) rename modules/swagger-codegen/src/test/resources/{integrationtest => integrationtests}/typescript/additional-properties-expected/model/User.ts (100%) rename modules/swagger-codegen/src/test/resources/{integrationtest => integrationtests}/typescript/additional-properties-expected/model/models.ts (100%) rename modules/swagger-codegen/src/test/resources/{integrationtest => integrationtests}/typescript/additional-properties-expected/package.json (100%) rename modules/swagger-codegen/src/test/resources/{integrationtest => integrationtests}/typescript/additional-properties-expected/tsconfig.json (100%) rename modules/swagger-codegen/src/test/resources/{integrationtest => integrationtests}/typescript/additional-properties-expected/typings.json (100%) rename modules/swagger-codegen/src/test/resources/{integrationtest => integrationtests}/typescript/additional-properties-spec.json (100%) diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/AbstractIntegrationTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/AbstractIntegrationTest.java new file mode 100644 index 00000000000..faf5c3551ca --- /dev/null +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/AbstractIntegrationTest.java @@ -0,0 +1,45 @@ +package io.swagger.codegen; + +import org.testng.annotations.Test; +import org.testng.reporters.Files; + +import java.io.IOException; +import java.util.Map; + +import io.swagger.codegen.utils.IntegrationTestPathsConfig; +import io.swagger.models.Swagger; +import io.swagger.parser.SwaggerParser; + +import static io.swagger.codegen.utils.AssertFile.assertPathEqualsRecursively; + +public abstract class AbstractIntegrationTest { + + protected abstract IntegrationTestPathsConfig getIntegrationTestPathsConfig(); + + protected abstract CodegenConfig getCodegenConfig(); + + protected abstract Map configProperties(); + + @Test + public void generatesCorrectDirectoryStructure() throws IOException { + DefaultGenerator codeGen = new DefaultGenerator(); + IntegrationTestPathsConfig integrationTestPathsConfig = getIntegrationTestPathsConfig(); + + String specContent = Files.readFile(integrationTestPathsConfig.getSpecPath().toFile()); + Swagger swagger = new SwaggerParser().parse(specContent); + + CodegenConfig codegenConfig = getCodegenConfig(); + codegenConfig.setOutputDir(integrationTestPathsConfig.getOutputPath().toString()); + + ClientOpts clientOpts = new ClientOpts(); + clientOpts.setProperties(configProperties()); + ClientOptInput opts = new ClientOptInput() + .config(codegenConfig) + .opts(clientOpts) + .swagger(swagger); + + codeGen.opts(opts).generate(); + + assertPathEqualsRecursively(integrationTestPathsConfig.getExpectedPath(), integrationTestPathsConfig.getOutputPath()); + } +} diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/Integrationtest/Angular2GenerationWithAditionPropertiesTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/Integrationtest/Angular2GenerationWithAditionPropertiesTest.java deleted file mode 100644 index 98878490fa6..00000000000 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/Integrationtest/Angular2GenerationWithAditionPropertiesTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package io.swagger.codegen.typescript.integrationtest; - -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; -import org.testng.reporters.Files; - -import java.io.IOException; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.HashMap; -import java.util.Map; - -import io.swagger.codegen.ClientOptInput; -import io.swagger.codegen.ClientOpts; -import io.swagger.codegen.CodegenConfig; -import io.swagger.codegen.DefaultGenerator; -import io.swagger.codegen.languages.TypeScriptAngular2ClientCodegen; -import io.swagger.models.Swagger; -import io.swagger.parser.SwaggerParser; - -import static io.swagger.codegen.typescript.integrationtest.AssertFile.assertPathEqualsRecursively; - -public class Angular2GenerationWithAditionPropertiesTest { - - private DefaultGenerator codeGen; - private Path integrationTestPath; - private Path outputPath; - private Path specPath; - private Path expectedPath; - - @BeforeMethod - public void setUp() { - codeGen = new DefaultGenerator(); - integrationTestPath = Paths.get("target/test-classes/integrationtest").toAbsolutePath(); - outputPath = integrationTestPath.resolve("typescript/additional-properties-result"); - expectedPath = integrationTestPath.resolve("typescript/additional-properties-expected"); - specPath = integrationTestPath.resolve("typescript/additional-properties-spec.json"); - - } - - protected CodegenConfig getCodegenConfig() { - return new TypeScriptAngular2ClientCodegen(); - } - - protected Map configProperties() { - Map propeties = new HashMap<>(); - propeties.put("npmName", "additionalPropertiesTest"); - propeties.put("npmVersion", "1.0.2"); - propeties.put("snapshot", "false"); - - return propeties; - } - - @Test(description = "The correct output is generated for a spec with additional-properties") - public void shouldGenerateCorrectTypescriptModels() throws IOException { - String specContent = Files.readFile(specPath.toFile()); - Swagger swagger = new SwaggerParser().parse(specContent); - - CodegenConfig codegenConfig = getCodegenConfig(); - codegenConfig.setOutputDir(outputPath.toString()); - - ClientOpts clientOpts = new ClientOpts(); - clientOpts.setProperties(configProperties()); - ClientOptInput opts = new ClientOptInput() - .config(codegenConfig) - .opts(clientOpts) - .swagger(swagger); - - codeGen.opts(opts).generate(); - - assertPathEqualsRecursively(expectedPath, outputPath); - } - -} diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypescriptAngular2AdditionalPropertiesIntegrationTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypescriptAngular2AdditionalPropertiesIntegrationTest.java new file mode 100644 index 00000000000..f8e89a592ed --- /dev/null +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypescriptAngular2AdditionalPropertiesIntegrationTest.java @@ -0,0 +1,31 @@ +package io.swagger.codegen.typescript.typescriptangular2; + +import java.util.HashMap; +import java.util.Map; + +import io.swagger.codegen.CodegenConfig; +import io.swagger.codegen.languages.TypeScriptAngular2ClientCodegen; +import io.swagger.codegen.utils.IntegrationTestPathsConfig; + +public class TypescriptAngular2AdditionalPropertiesIntegrationTest extends io.swagger.codegen.AbstractIntegrationTest { + + @Override + protected CodegenConfig getCodegenConfig() { + return new TypeScriptAngular2ClientCodegen(); + } + + @Override + protected Map configProperties() { + Map propeties = new HashMap<>(); + propeties.put("npmName", "additionalPropertiesTest"); + propeties.put("npmVersion", "1.0.2"); + propeties.put("snapshot", "false"); + + return propeties; + } + + @Override + protected IntegrationTestPathsConfig getIntegrationTestPathsConfig() { + return new IntegrationTestPathsConfig("typescript/additional-properties"); + } +} diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/integrationtest/AssertFile.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/AssertFile.java similarity index 80% rename from modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/integrationtest/AssertFile.java rename to modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/AssertFile.java index 6f78c80a755..5ec2d279d7d 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/integrationtest/AssertFile.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/AssertFile.java @@ -1,4 +1,4 @@ -package io.swagger.codegen.typescript.integrationtest; +package io.swagger.codegen.utils; import org.testng.Assert; @@ -48,12 +48,12 @@ public class AssertFile { Path actualDir = absoluteActual.resolve(relativeExpectedDir); if (!Files.exists(actualDir)) { - fail(String.format("Directory \'%s\' missing in target.", expectedDir.getFileName())); + fail(String.format("Directory '%s' is missing.", actualDir)); } - assertEquals(expectedDir.toFile().list().length, - actualDir.toFile().list().length, - String.format("Directory size of \'%s\' differ. ", relativeExpectedDir)); + assertEquals(expectedDir.toFile().list(), + actualDir.toFile().list(), + String.format("Directory content of '%s' and '%s' differ.", expectedDir, actualDir)); return FileVisitResult.CONTINUE; } @@ -64,11 +64,11 @@ public class AssertFile { Path actualFile = absoluteActual.resolve(relativeExpectedFile); if (!Files.exists(actualFile)) { - fail(String.format("File \'%s\' missing in target.", expectedFile.getFileName())); + fail(String.format("File '%s' is missing.", actualFile)); } assertEquals(Files.readAllLines(expectedFile, Charset.defaultCharset()), - Files.readAllLines(actualFile, Charset.defaultCharset()), - String.format("File content of \'%s\' differ. ", relativeExpectedFile)); + Files.readAllLines(actualFile, Charset.defaultCharset()), + String.format("File content of '%s' and '%s' differ.", expectedFile, actualFile)); return FileVisitResult.CONTINUE; } diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/IntegrationTestPathsConfig.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/IntegrationTestPathsConfig.java new file mode 100644 index 00000000000..25aca976e61 --- /dev/null +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/IntegrationTestPathsConfig.java @@ -0,0 +1,33 @@ +package io.swagger.codegen.utils; + +import java.nio.file.Path; +import java.nio.file.Paths; + +public class IntegrationTestPathsConfig { + private static final Path INTEGRATION_TEST_PATH = Paths.get("target/test-classes/integrationtests").toAbsolutePath(); + private final Path outputPath; + private final Path specPath; + private final Path expectedPath; + + public IntegrationTestPathsConfig(String location) { + this(location + "-spec.json", location + "-result", location + "-expected"); + } + + public IntegrationTestPathsConfig(String specLocation, String outputLocation, String expectedLocation) { + outputPath = INTEGRATION_TEST_PATH.resolve(outputLocation); + expectedPath = INTEGRATION_TEST_PATH.resolve(expectedLocation); + specPath = INTEGRATION_TEST_PATH.resolve(specLocation); + } + + public Path getOutputPath() { + return outputPath; + } + + public Path getSpecPath() { + return specPath; + } + + public Path getExpectedPath() { + return expectedPath; + } +} \ No newline at end of file diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/api/api.ts b/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/api/api.ts deleted file mode 100644 index 5cae4dbb428..00000000000 --- a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/api/api.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from '../api/UserApi'; - - diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/README.md b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/README.md similarity index 100% rename from modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/README.md rename to modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/README.md diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/api/UserApi.ts b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/UserApi.ts similarity index 100% rename from modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/api/UserApi.ts rename to modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/UserApi.ts diff --git a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/api.ts b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/api.ts new file mode 100644 index 00000000000..b261015b7e3 --- /dev/null +++ b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/api.ts @@ -0,0 +1,3 @@ +export * from './UserApi'; + + diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/index.ts b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/index.ts similarity index 100% rename from modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/index.ts rename to modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/index.ts diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/model/User.ts b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/User.ts similarity index 100% rename from modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/model/User.ts rename to modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/User.ts diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/model/models.ts b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/models.ts similarity index 100% rename from modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/model/models.ts rename to modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/models.ts diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/package.json b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/package.json similarity index 100% rename from modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/package.json rename to modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/package.json diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/tsconfig.json b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/tsconfig.json similarity index 100% rename from modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/tsconfig.json rename to modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/tsconfig.json diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/typings.json b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/typings.json similarity index 100% rename from modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-expected/typings.json rename to modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/typings.json diff --git a/modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-spec.json b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-spec.json similarity index 100% rename from modules/swagger-codegen/src/test/resources/integrationtest/typescript/additional-properties-spec.json rename to modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-spec.json From d78b5f177380c3a2b4c2900572fea37fbb069e9a Mon Sep 17 00:00:00 2001 From: Kristof Vrolijkx Date: Sun, 1 May 2016 22:45:06 +0200 Subject: [PATCH 03/10] adding diff for files in integration test --- modules/swagger-codegen/pom.xml | 10 ++++ .../typescript-angular2/apis.mustache | 6 +-- .../typescript-angular2/models.mustache | 3 -- .../io/swagger/codegen/utils/AssertFile.java | 50 ++++++++++++++++--- .../api/UserApi.ts | 1 - .../additional-properties-expected/api/api.ts | 2 - .../additional-properties-expected/index.ts | 4 +- .../model/User.ts | 1 - .../model/models.ts | 3 -- 9 files changed, 58 insertions(+), 22 deletions(-) diff --git a/modules/swagger-codegen/pom.xml b/modules/swagger-codegen/pom.xml index afaecfc3512..781a789372e 100644 --- a/modules/swagger-codegen/pom.xml +++ b/modules/swagger-codegen/pom.xml @@ -199,6 +199,9 @@ + + 1.2.1 + io.swagger @@ -279,6 +282,13 @@ test + + com.googlecode.java-diff-utils + diffutils + ${diffutils-version} + test + + diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/apis.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/apis.mustache index 05b5c6ec2ea..9a39b864538 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular2/apis.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/apis.mustache @@ -1,9 +1,7 @@ {{#apiInfo}} {{#apis}} {{#operations}} -export * from '../api/{{classname}}'; +export * from './{{ classname }}'; {{/operations}} {{/apis}} -{{/apiInfo}} - - +{{/apiInfo}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/models.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/models.mustache index 677b6b87328..ace053bd55b 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular2/models.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/models.mustache @@ -3,6 +3,3 @@ export * from './{{{ classname }}}'; {{/model}} {{/models}} - - - diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/AssertFile.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/AssertFile.java index 5ec2d279d7d..d08b53bd686 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/AssertFile.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/AssertFile.java @@ -9,6 +9,11 @@ import java.nio.file.FileVisitor; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; +import java.util.List; + +import difflib.Delta; +import difflib.DiffUtils; +import difflib.Patch; import static org.testng.Assert.assertEquals; import static org.testng.Assert.fail; @@ -27,14 +32,14 @@ public class AssertFile { /** * Asserts that two directories are recursively equal. If they are not, an {@link AssertionError} is thrown with the * given message.
- * There will be a binary comparison of all files under expected with all files under actual. File attributes will + * There will be a textual comparison of all files under expected with all files under actual. File attributes will * not be considered.
* Missing or additional files are considered an error.
* * @param expected Path expected directory * @param actual Path actual directory */ - public static final void assertPathEqualsRecursively(final Path expected, final Path actual) { + public static void assertPathEqualsRecursively(final Path expected, final Path actual) { Assert.assertNotNull(expected); Assert.assertNotNull(actual); final Path absoluteExpected = expected.toAbsolutePath(); @@ -66,9 +71,8 @@ public class AssertFile { if (!Files.exists(actualFile)) { fail(String.format("File '%s' is missing.", actualFile)); } - assertEquals(Files.readAllLines(expectedFile, Charset.defaultCharset()), - Files.readAllLines(actualFile, Charset.defaultCharset()), - String.format("File content of '%s' and '%s' differ.", expectedFile, actualFile)); + + assertFilesAreEqual(expectedFile, actualFile); return FileVisitResult.CONTINUE; } @@ -86,9 +90,43 @@ public class AssertFile { }); } catch (IOException e) { - fail(e.getMessage()); + fail(e.getMessage(), e); } } + + public static void assertFilesAreEqual(final Path expected, final Path actual) { + + if(!Files.isRegularFile(expected)) { + fail("expected: '%s' is not a readable file"); + } + + if(!Files.isRegularFile(actual)) { + fail("actual: '%s' is not a readable file"); + } + + try { + List expectedLines = Files.readAllLines(expected, Charset.defaultCharset()); + List actualLines = Files.readAllLines(actual, Charset.defaultCharset()); + Patch diff = DiffUtils.diff(expectedLines, actualLines); + List deltas = diff.getDeltas(); + if(!deltas.isEmpty()) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("files diff:\n"); + stringBuilder.append("\tfile: '" + expected.toAbsolutePath().toString() + "' \n"); + stringBuilder.append("\tfile: '" + actual.toAbsolutePath().toString() + "' \n"); + stringBuilder.append("\tdiffs:\n"); + + for (Delta delta: deltas) { + stringBuilder.append(delta.toString() + "\n"); + } + + fail(stringBuilder.toString()); + } + + } catch (IOException e) { + fail(e.getMessage(), e); + } + } } diff --git a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/UserApi.ts b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/UserApi.ts index 40f94703038..b5dea99577c 100644 --- a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/UserApi.ts +++ b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/UserApi.ts @@ -61,4 +61,3 @@ export class UserApi { } } - diff --git a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/api.ts b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/api.ts index b261015b7e3..d3bd8432806 100644 --- a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/api.ts +++ b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/api.ts @@ -1,3 +1 @@ export * from './UserApi'; - - diff --git a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/index.ts b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/index.ts index d6d02862e3c..cdfea183ad3 100644 --- a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/index.ts +++ b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/index.ts @@ -1,2 +1,2 @@ -export * from 'api/api'; -export * from 'model/models'; \ No newline at end of file +export * from './api/api'; +export * from './model/models'; diff --git a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/User.ts b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/User.ts index b1ce0a0f144..44842ba89ee 100644 --- a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/User.ts +++ b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/User.ts @@ -11,4 +11,3 @@ export interface User { */ userStatus?: number; } - diff --git a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/models.ts b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/models.ts index 115b5f75ce4..f6b9f36c6e1 100644 --- a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/models.ts +++ b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/models.ts @@ -1,4 +1 @@ export * from './User'; - - - From 37ae53c2cc7bf9e0a81fd28debb9c01e6ddd48ee Mon Sep 17 00:00:00 2001 From: Kristof Vrolijkx Date: Mon, 16 May 2016 13:45:12 +0200 Subject: [PATCH 04/10] add support for additional properties in codegen --- .../java/io/swagger/codegen/CodegenModel.java | 8 +- .../io/swagger/codegen/DefaultCodegen.java | 80 ++++++++++++++++--- .../TypeScriptAngular2ClientCodegen.java | 9 ++- .../typescript-angular2/model.mustache | 4 +- ...r2AdditionalPropertiesIntegrationTest.java | 3 +- .../additional-properties-expected/README.md | 4 +- .../package.json | 2 +- 7 files changed, 89 insertions(+), 21 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java index ccea2ee070e..453d4e88c65 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java @@ -1,7 +1,12 @@ package io.swagger.codegen; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + import io.swagger.models.ExternalDocs; -import java.util.*; public class CodegenModel { public String parent, parentSchema; @@ -31,6 +36,7 @@ public class CodegenModel { public ExternalDocs externalDocs; public Map vendorExtensions; + public String additionalPropertiesType; { // By default these are the same collections. Where the code generator supports inheritance, composed models diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index 5d71a347df0..231f1ef0210 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -2,25 +2,76 @@ package io.swagger.codegen; import com.google.common.base.Function; import com.google.common.collect.Lists; -import io.swagger.codegen.examples.ExampleGenerator; -import io.swagger.models.*; -import io.swagger.models.auth.*; -import io.swagger.models.parameters.*; -import io.swagger.models.properties.*; -import io.swagger.models.properties.PropertyBuilder.PropertyId; -import io.swagger.util.Json; + import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.annotation.Nullable; import java.io.File; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; +import java.util.Set; +import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.annotation.Nullable; + +import io.swagger.codegen.examples.ExampleGenerator; +import io.swagger.models.ArrayModel; +import io.swagger.models.ComposedModel; +import io.swagger.models.Model; +import io.swagger.models.ModelImpl; +import io.swagger.models.Operation; +import io.swagger.models.RefModel; +import io.swagger.models.Response; +import io.swagger.models.Swagger; +import io.swagger.models.auth.ApiKeyAuthDefinition; +import io.swagger.models.auth.BasicAuthDefinition; +import io.swagger.models.auth.In; +import io.swagger.models.auth.OAuth2Definition; +import io.swagger.models.auth.SecuritySchemeDefinition; +import io.swagger.models.parameters.BodyParameter; +import io.swagger.models.parameters.CookieParameter; +import io.swagger.models.parameters.FormParameter; +import io.swagger.models.parameters.HeaderParameter; +import io.swagger.models.parameters.Parameter; +import io.swagger.models.parameters.PathParameter; +import io.swagger.models.parameters.QueryParameter; +import io.swagger.models.parameters.SerializableParameter; +import io.swagger.models.properties.AbstractNumericProperty; +import io.swagger.models.properties.ArrayProperty; +import io.swagger.models.properties.BaseIntegerProperty; +import io.swagger.models.properties.BinaryProperty; +import io.swagger.models.properties.BooleanProperty; +import io.swagger.models.properties.ByteArrayProperty; +import io.swagger.models.properties.DateProperty; +import io.swagger.models.properties.DateTimeProperty; +import io.swagger.models.properties.DecimalProperty; +import io.swagger.models.properties.DoubleProperty; +import io.swagger.models.properties.FloatProperty; +import io.swagger.models.properties.IntegerProperty; +import io.swagger.models.properties.LongProperty; +import io.swagger.models.properties.MapProperty; +import io.swagger.models.properties.Property; +import io.swagger.models.properties.PropertyBuilder; +import io.swagger.models.properties.PropertyBuilder.PropertyId; +import io.swagger.models.properties.RefProperty; +import io.swagger.models.properties.StringProperty; +import io.swagger.models.properties.UUIDProperty; +import io.swagger.util.Json; + public class DefaultCodegen { protected static final Logger LOGGER = LoggerFactory.getLogger(DefaultCodegen.class); @@ -1207,8 +1258,7 @@ public class DefaultCodegen { m.dataType = getSwaggerType(p); } if (impl.getAdditionalProperties() != null) { - MapProperty mapProperty = new MapProperty(impl.getAdditionalProperties()); - addParentContainer(m, name, mapProperty); + addAdditionPropertiesToCodeGenModel(m, impl); } addVars(m, impl.getProperties(), impl.getRequired()); } @@ -1221,8 +1271,12 @@ public class DefaultCodegen { return m; } - protected void addProperties(Map properties, List required, Model model, - Map allDefinitions) { + protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, ModelImpl swaggerModel) { + MapProperty mapProperty = new MapProperty(swaggerModel.getAdditionalProperties()); + addParentContainer(codegenModel, codegenModel.name, mapProperty); + } + + protected void addProperties(Map properties, List required, Model model, Map allDefinitions) { if (model instanceof ModelImpl) { ModelImpl mi = (ModelImpl) model; diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java index a80e0f66ae0..56435112b94 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java @@ -5,8 +5,10 @@ import java.text.SimpleDateFormat; import java.util.Date; import io.swagger.codegen.CliOption; +import io.swagger.codegen.CodegenModel; import io.swagger.codegen.CodegenParameter; import io.swagger.codegen.SupportingFile; +import io.swagger.models.ModelImpl; import io.swagger.models.properties.ArrayProperty; import io.swagger.models.properties.BooleanProperty; import io.swagger.models.properties.FileProperty; @@ -43,6 +45,11 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod this.cliOptions.add(new CliOption(SNAPSHOT, "When setting this property to true the version will be suffixed with -SNAPSHOT.yyyyMMddHHmm", BooleanProperty.TYPE).defaultValue(Boolean.FALSE.toString())); } + @Override + protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, ModelImpl swaggerModel) { + codegenModel.additionalPropertiesType = getSwaggerType(swaggerModel.getAdditionalProperties()); + } + @Override public String getName() { return "typescript-angular2"; @@ -131,7 +138,7 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod @Override public void postProcessParameter(CodegenParameter parameter) { super.postProcessParameter(parameter); - parameter.dataType = addModelPrefix(parameter.dataType); + parameter.dataType = addModelPrefix(parameter.dataType); } public String getNpmName() { diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/model.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/model.mustache index 4170b2d1594..eadcf9aa5bd 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular2/model.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/model.mustache @@ -9,6 +9,7 @@ import * as models from './models'; */ {{/description}} export interface {{classname}} {{#parent}}extends models.{{{parent}}} {{/parent}}{ + {{#additionalPropertiesType}}[key: string]: {{{additionalPropertiesType}}}{{/additionalPropertiesType}} {{#vars}} {{#description}} @@ -19,7 +20,6 @@ export interface {{classname}} {{#parent}}extends models.{{{parent}}} {{/parent} {{name}}?: {{#isEnum}}{{classname}}.{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}}; {{/vars}} } - {{#hasEnums}} export namespace {{classname}} { {{#vars}} @@ -33,4 +33,4 @@ export namespace {{classname}} { } {{/hasEnums}} {{/model}} -{{/models}} +{{/models}} \ No newline at end of file diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypescriptAngular2AdditionalPropertiesIntegrationTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypescriptAngular2AdditionalPropertiesIntegrationTest.java index f8e89a592ed..8cc7f25c023 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypescriptAngular2AdditionalPropertiesIntegrationTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypescriptAngular2AdditionalPropertiesIntegrationTest.java @@ -3,11 +3,12 @@ package io.swagger.codegen.typescript.typescriptangular2; import java.util.HashMap; import java.util.Map; +import io.swagger.codegen.AbstractIntegrationTest; import io.swagger.codegen.CodegenConfig; import io.swagger.codegen.languages.TypeScriptAngular2ClientCodegen; import io.swagger.codegen.utils.IntegrationTestPathsConfig; -public class TypescriptAngular2AdditionalPropertiesIntegrationTest extends io.swagger.codegen.AbstractIntegrationTest { +public class TypescriptAngular2AdditionalPropertiesIntegrationTest extends AbstractIntegrationTest { @Override protected CodegenConfig getCodegenConfig() { diff --git a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/README.md b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/README.md index 92aa291c7aa..b5338574869 100644 --- a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/README.md +++ b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/README.md @@ -1,4 +1,4 @@ -## additionalPropertiesTest@1.0.0 +## additionalPropertiesTest@1.0.2 ### Building @@ -19,7 +19,7 @@ navigate to the folder of your consuming project and run one of next commando's. _published:_ ``` -npm install additionalPropertiesTest@1.0.0 --save +npm install additionalPropertiesTest@1.0.2 --save ``` _unPublished (not recommended):_ diff --git a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/package.json b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/package.json index b75234c322a..54b5bc9a590 100644 --- a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/package.json +++ b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/package.json @@ -1,6 +1,6 @@ { "name": "additionalPropertiesTest", - "version": "1.0.0", + "version": "1.0.2", "description": "swagger client for additionalPropertiesTest", "author": "Swagger Codegen Contributors", "keywords": [ From c018936c15ab810cfa3e2804a4322d70b5f47358 Mon Sep 17 00:00:00 2001 From: Kristof Vrolijkx Date: Mon, 16 May 2016 13:47:09 +0200 Subject: [PATCH 05/10] add support for additional properties in codegen --- .../src/main/java/io/swagger/codegen/CodegenModel.java | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java index 453d4e88c65..41ca44f7bac 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java @@ -8,6 +8,7 @@ import java.util.TreeSet; import io.swagger.models.ExternalDocs; + public class CodegenModel { public String parent, parentSchema; public List interfaces; From 68d47be9fd308aff3a16334f610d80af9811f86e Mon Sep 17 00:00:00 2001 From: Kristof Vrolijkx Date: Mon, 16 May 2016 17:51:00 +0200 Subject: [PATCH 06/10] remove not needed peer dependencies and add any type too additional properties if also other properties defined. --- .../typescript-angular2/model.mustache | 2 +- .../api/UserApi.ts | 7 ++++--- .../model/User.ts | 2 +- .../package.json | 18 ++++++++++++------ 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/model.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/model.mustache index eadcf9aa5bd..e8ce6c3642b 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular2/model.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/model.mustache @@ -9,7 +9,7 @@ import * as models from './models'; */ {{/description}} export interface {{classname}} {{#parent}}extends models.{{{parent}}} {{/parent}}{ - {{#additionalPropertiesType}}[key: string]: {{{additionalPropertiesType}}}{{/additionalPropertiesType}} + {{#additionalPropertiesType}}[key: string]: {{{additionalPropertiesType}}}{{#hasVars}} | any{{/hasVars}};{{/additionalPropertiesType}} {{#vars}} {{#description}} diff --git a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/UserApi.ts b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/UserApi.ts index b5dea99577c..77b6c013a8b 100644 --- a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/UserApi.ts +++ b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/api/UserApi.ts @@ -1,7 +1,8 @@ -import {Http, Headers, RequestOptionsArgs, Response, URLSearchParams} from 'angular2/http'; -import {Injectable} from 'angular2/core'; +import {Http, Headers, RequestOptionsArgs, Response, URLSearchParams} from '@angular/http'; +import {Injectable, Optional} from '@angular/core'; import {Observable} from 'rxjs/Observable'; import * as models from '../model/models'; +import 'rxjs/Rx'; /* tslint:disable:no-unused-variable member-ordering */ @@ -12,7 +13,7 @@ export class UserApi { protected basePath = 'http://additional-properties.swagger.io/v2'; public defaultHeaders : Headers = new Headers(); - constructor(protected http: Http, basePath: string) { + constructor(protected http: Http, @Optional() basePath: string) { if (basePath) { this.basePath = basePath; } diff --git a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/User.ts b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/User.ts index 44842ba89ee..66f270cea01 100644 --- a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/User.ts +++ b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/model/User.ts @@ -2,7 +2,7 @@ import * as models from './models'; export interface User { - [key: string]: string + [key: string]: string | any; id?: number; diff --git a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/package.json b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/package.json index 54b5bc9a590..36ec153321d 100644 --- a/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/package.json +++ b/modules/swagger-codegen/src/test/resources/integrationtests/typescript/additional-properties-expected/package.json @@ -16,15 +16,21 @@ "build": "typings install && tsc" }, "peerDependencies": { - "angular2": "^2.0.0-beta.15", - "rxjs": "^5.0.0-beta.2" + "@angular/core": "^2.0.0-rc.1", + "@angular/http": "^2.0.0-rc.1" }, "devDependencies": { + "@angular/common": "^2.0.0-rc.1", + "@angular/compiler": "^2.0.0-rc.1", + "@angular/core": "^2.0.0-rc.1", + "@angular/http": "^2.0.0-rc.1", + "@angular/platform-browser": "^2.0.0-rc.1", + "@angular/platform-browser-dynamic": "^2.0.0-rc.1", + "core-js": "^2.3.0", + "rxjs": "^5.0.0-beta.6", + "zone.js": "^0.6.12", "typescript": "^1.8.10", "typings": "^0.8.1", - "angular2": "^2.0.0-beta.15", "es6-shim": "^0.35.0", - "es7-reflect-metadata": "^1.6.0", - "rxjs": "5.0.0-beta.2", - "zone.js": "^0.6.10" + "es7-reflect-metadata": "^1.6.0" }} From ec65eb5975fac246d7cc60784c03bec4f2bdab3e Mon Sep 17 00:00:00 2001 From: Kristof Vrolijkx Date: Mon, 16 May 2016 17:58:06 +0200 Subject: [PATCH 07/10] some small optimizations --- .../main/resources/typescript-angular2/package.mustache | 9 +-------- .../java/io/swagger/codegen/AbstractIntegrationTest.java | 4 ++-- .../swagger/codegen/{utils => testutils}/AssertFile.java | 2 +- .../{utils => testutils}/IntegrationTestPathsConfig.java | 2 +- ...criptAngular2AdditionalPropertiesIntegrationTest.java | 2 +- 5 files changed, 6 insertions(+), 13 deletions(-) rename modules/swagger-codegen/src/test/java/io/swagger/codegen/{utils => testutils}/AssertFile.java (99%) rename modules/swagger-codegen/src/test/java/io/swagger/codegen/{utils => testutils}/IntegrationTestPathsConfig.java (96%) diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/package.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/package.mustache index 547200a6695..28a18bfb91b 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular2/package.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/package.mustache @@ -16,15 +16,8 @@ "build": "typings install && tsc" }, "peerDependencies": { - "@angular/common": "^2.0.0-rc.1", - "@angular/compiler": "^2.0.0-rc.1", "@angular/core": "^2.0.0-rc.1", - "@angular/http": "^2.0.0-rc.1", - "@angular/platform-browser": "^2.0.0-rc.1", - "@angular/platform-browser-dynamic": "^2.0.0-rc.1", - "core-js": "^2.3.0", - "rxjs": "^5.0.0-beta.6", - "zone.js": "^0.6.12" + "@angular/http": "^2.0.0-rc.1" }, "devDependencies": { "@angular/common": "^2.0.0-rc.1", diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/AbstractIntegrationTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/AbstractIntegrationTest.java index faf5c3551ca..fa4d2e9da53 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/AbstractIntegrationTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/AbstractIntegrationTest.java @@ -6,11 +6,11 @@ import org.testng.reporters.Files; import java.io.IOException; import java.util.Map; -import io.swagger.codegen.utils.IntegrationTestPathsConfig; +import io.swagger.codegen.testutils.IntegrationTestPathsConfig; import io.swagger.models.Swagger; import io.swagger.parser.SwaggerParser; -import static io.swagger.codegen.utils.AssertFile.assertPathEqualsRecursively; +import static io.swagger.codegen.testutils.AssertFile.assertPathEqualsRecursively; public abstract class AbstractIntegrationTest { diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/AssertFile.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/testutils/AssertFile.java similarity index 99% rename from modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/AssertFile.java rename to modules/swagger-codegen/src/test/java/io/swagger/codegen/testutils/AssertFile.java index d08b53bd686..f810e20eb0a 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/AssertFile.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/testutils/AssertFile.java @@ -1,4 +1,4 @@ -package io.swagger.codegen.utils; +package io.swagger.codegen.testutils; import org.testng.Assert; diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/IntegrationTestPathsConfig.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/testutils/IntegrationTestPathsConfig.java similarity index 96% rename from modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/IntegrationTestPathsConfig.java rename to modules/swagger-codegen/src/test/java/io/swagger/codegen/testutils/IntegrationTestPathsConfig.java index 25aca976e61..4335c69dd2d 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/utils/IntegrationTestPathsConfig.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/testutils/IntegrationTestPathsConfig.java @@ -1,4 +1,4 @@ -package io.swagger.codegen.utils; +package io.swagger.codegen.testutils; import java.nio.file.Path; import java.nio.file.Paths; diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypescriptAngular2AdditionalPropertiesIntegrationTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypescriptAngular2AdditionalPropertiesIntegrationTest.java index 8cc7f25c023..8b23105f28c 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypescriptAngular2AdditionalPropertiesIntegrationTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypescriptAngular2AdditionalPropertiesIntegrationTest.java @@ -6,7 +6,7 @@ import java.util.Map; import io.swagger.codegen.AbstractIntegrationTest; import io.swagger.codegen.CodegenConfig; import io.swagger.codegen.languages.TypeScriptAngular2ClientCodegen; -import io.swagger.codegen.utils.IntegrationTestPathsConfig; +import io.swagger.codegen.testutils.IntegrationTestPathsConfig; public class TypescriptAngular2AdditionalPropertiesIntegrationTest extends AbstractIntegrationTest { From 5b1c779e5768ac4e882209a5d26ee2676d1c09fb Mon Sep 17 00:00:00 2001 From: Kristof Vrolijkx Date: Mon, 16 May 2016 20:33:49 +0200 Subject: [PATCH 08/10] fix unit test --- .../src/main/java/io/swagger/codegen/DefaultCodegen.java | 2 +- .../codegen/languages/TypeScriptAngular2ClientCodegen.java | 1 + .../typescriptangular2/TypeScriptAngular2ModelTest.java | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index cb7164cc1f9..15eed39154a 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -2514,7 +2514,7 @@ public class DefaultCodegen { } } - private void addImport(CodegenModel m, String type) { + protected void addImport(CodegenModel m, String type) { if (type != null && needToImport(type)) { m.imports.add(type); } diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java index 56435112b94..0b1df6bfaf5 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java @@ -48,6 +48,7 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod @Override protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, ModelImpl swaggerModel) { codegenModel.additionalPropertiesType = getSwaggerType(swaggerModel.getAdditionalProperties()); + addImport(codegenModel, codegenModel.additionalPropertiesType); } @Override diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypeScriptAngular2ModelTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypeScriptAngular2ModelTest.java index 52e291de676..685495f03c2 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypeScriptAngular2ModelTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/typescriptangular2/TypeScriptAngular2ModelTest.java @@ -178,6 +178,7 @@ public class TypeScriptAngular2ModelTest { Assert.assertEquals(cm.description, "a map model"); Assert.assertEquals(cm.vars.size(), 0); Assert.assertEquals(cm.imports.size(), 1); + Assert.assertEquals(cm.additionalPropertiesType, "models.Children"); Assert.assertEquals(Sets.intersection(cm.imports, Sets.newHashSet("models.Children")).size(), 1); } } From ca2174f079825ad138ace14639c9971efc9d824c Mon Sep 17 00:00:00 2001 From: Kristof Vrolijkx Date: Mon, 16 May 2016 20:42:42 +0200 Subject: [PATCH 09/10] small code movement --- .../fetch}/TypeScriptFetchClientOptionsTest.java | 2 +- .../fetch}/TypeScriptFetchModelTest.java | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) rename modules/swagger-codegen/src/test/java/io/swagger/codegen/{typescriptfetch => typescript/fetch}/TypeScriptFetchClientOptionsTest.java (96%) rename modules/swagger-codegen/src/test/java/io/swagger/codegen/{typescriptfetch => typescript/fetch}/TypeScriptFetchModelTest.java (96%) diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptfetch/TypeScriptFetchClientOptionsTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/fetch/TypeScriptFetchClientOptionsTest.java similarity index 96% rename from modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptfetch/TypeScriptFetchClientOptionsTest.java rename to modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/fetch/TypeScriptFetchClientOptionsTest.java index 09f16799ad9..c2744cc8258 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptfetch/TypeScriptFetchClientOptionsTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/fetch/TypeScriptFetchClientOptionsTest.java @@ -1,4 +1,4 @@ -package io.swagger.codegen.typescriptfetch; +package io.swagger.codegen.typescript.fetch; import io.swagger.codegen.AbstractOptionsTest; import io.swagger.codegen.CodegenConfig; diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptfetch/TypeScriptFetchModelTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/fetch/TypeScriptFetchModelTest.java similarity index 96% rename from modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptfetch/TypeScriptFetchModelTest.java rename to modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/fetch/TypeScriptFetchModelTest.java index d2173fdb710..d1b0a4be233 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescriptfetch/TypeScriptFetchModelTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/typescript/fetch/TypeScriptFetchModelTest.java @@ -1,6 +1,10 @@ -package io.swagger.codegen.typescriptfetch; +package io.swagger.codegen.typescript.fetch; import com.google.common.collect.Sets; + +import org.testng.Assert; +import org.testng.annotations.Test; + import io.swagger.codegen.CodegenModel; import io.swagger.codegen.CodegenProperty; import io.swagger.codegen.DefaultCodegen; @@ -8,9 +12,11 @@ import io.swagger.codegen.languages.TypeScriptFetchClientCodegen; import io.swagger.models.ArrayModel; import io.swagger.models.Model; import io.swagger.models.ModelImpl; -import io.swagger.models.properties.*; -import org.testng.Assert; -import org.testng.annotations.Test; +import io.swagger.models.properties.ArrayProperty; +import io.swagger.models.properties.DateTimeProperty; +import io.swagger.models.properties.LongProperty; +import io.swagger.models.properties.RefProperty; +import io.swagger.models.properties.StringProperty; @SuppressWarnings("static-method") public class TypeScriptFetchModelTest { From 816ba6fb390cbe31d52122181f5cbf16dfc2e51b Mon Sep 17 00:00:00 2001 From: Kristof Vrolijkx Date: Wed, 18 May 2016 08:42:37 +0200 Subject: [PATCH 10/10] Code review remarks --- .../java/io/swagger/codegen/ClientOpts.java | 50 ++++++++++++++++--- .../java/io/swagger/codegen/CodegenModel.java | 2 + .../TypeScriptAngular2ClientCodegen.java | 2 +- ...ith-fake-endpoints-models-for-testing.yaml | 16 ++++++ 4 files changed, 63 insertions(+), 7 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/ClientOpts.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/ClientOpts.java index 8de7476c5e2..172a057ea43 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/ClientOpts.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/ClientOpts.java @@ -6,8 +6,27 @@ import java.util.Map; import io.swagger.codegen.auth.AuthMethod; public class ClientOpts { + protected String uri; + protected String target; protected AuthMethod auth; protected Map properties = new HashMap(); + protected String outputDirectory; + + public String getUri() { + return uri; + } + + public void setUri(String uri) { + this.uri = uri; + } + + public String getTarget() { + return target; + } + + public void setTarget(String target) { + this.target = target; + } public Map getProperties() { return properties; @@ -17,10 +36,19 @@ public class ClientOpts { this.properties = properties; } + public String getOutputDirectory() { + return outputDirectory; + } + + public void setOutputDirectory(String outputDirectory) { + this.outputDirectory = outputDirectory; + } + @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("ClientOpts: {\n"); + sb.append(" uri: ").append(uri).append(","); sb.append(" auth: ").append(auth).append(","); sb.append(properties); sb.append("}"); @@ -29,20 +57,30 @@ public class ClientOpts { @Override public boolean equals(Object o) { - if (this == o) { return true; } - if (o == null || getClass() != o.getClass()) { return false; } + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; ClientOpts that = (ClientOpts) o; - if (auth != null ? !auth.equals(that.auth) : that.auth != null) { return false; } - return getProperties().equals(that.getProperties()); + if (uri != null ? !uri.equals(that.uri) : that.uri != null) + return false; + if (target != null ? !target.equals(that.target) : that.target != null) + return false; + if (auth != null ? !auth.equals(that.auth) : that.auth != null) + return false; + if (properties != null ? !properties.equals(that.properties) : that.properties != null) + return false; + return outputDirectory != null ? outputDirectory.equals(that.outputDirectory) : that.outputDirectory == null; } @Override public int hashCode() { - int result = auth != null ? auth.hashCode() : 0; - result = 31 * result + getProperties().hashCode(); + int result = uri != null ? uri.hashCode() : 0; + result = 31 * result + (target != null ? target.hashCode() : 0); + result = 31 * result + (auth != null ? auth.hashCode() : 0); + result = 31 * result + (properties != null ? properties.hashCode() : 0); + result = 31 * result + (outputDirectory != null ? outputDirectory.hashCode() : 0); return result; } } diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java index 34c4124320f..05883df4cfc 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenModel.java @@ -37,6 +37,8 @@ public class CodegenModel { public ExternalDocs externalDocs; public Map vendorExtensions; + + //The type of the value from additional properties. Used in map like objects. public String additionalPropertiesType; { diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java index 0b1df6bfaf5..aa14a565d10 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java @@ -139,7 +139,7 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod @Override public void postProcessParameter(CodegenParameter parameter) { super.postProcessParameter(parameter); - parameter.dataType = addModelPrefix(parameter.dataType); + parameter.dataType = addModelPrefix(parameter.dataType); } public String getNpmName() { diff --git a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml index b9fa750d38a..6b4fc91b50e 100644 --- a/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/swagger-codegen/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml @@ -944,6 +944,22 @@ definitions: enum: - 1.1 - -1.2 + AdditionalPropertiesClass: + type: object + additionalProperties: + type: string + MixedPropertiesAndAdditionalPropertiesClass: + type: object + properties: + uuid: + type: string + format: uuid + dateTime: + type: string + format: date-time + additionalProperties: + type: string + $ref: '#/definitions/Animal' externalDocs: description: Find out more about Swagger url: 'http://swagger.io'