diff --git a/bin/configs/javascript-es6.yaml b/bin/configs/javascript-es6.yaml index 67f013b0c51..bc9562f93e7 100644 --- a/bin/configs/javascript-es6.yaml +++ b/bin/configs/javascript-es6.yaml @@ -5,3 +5,10 @@ inputSpec: modules/openapi-generator/src/test/resources/3_0/javascript/petstore- templateDir: modules/openapi-generator/src/main/resources/Javascript additionalProperties: appName: PetstoreClient +modelNameMappings: + HealthCheckResult: HealthCheckStatus +parameterNameMappings: + query_1: queryOne +# the following has no effect as js client gen use baseName directly +nameMappings: + NullableMessage: nullable_field diff --git a/docs/customization.md b/docs/customization.md index ea91e18c4bc..29834b7f3bd 100644 --- a/docs/customization.md +++ b/docs/customization.md @@ -413,19 +413,18 @@ One can map the property name using `nameMappings` option and parameter name usi Here is an example to use `nameMappings` and `parameterNameMapping` in CLI: ```sh -java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml -o /tmp/java2/ --name-mappings _type=underscoreType, type_=typeWithUnderscore, --parameter-name-mappings _type=paramType, type_=typeParam +java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml -o /tmp/java2/ --name-mappings _type=underscoreType,type_=typeWithUnderscore, --parameter-name-mappings _type=paramType,type_=typeParam ``` To map model names, use `modelNameMappings` option, e.g. ```sh java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g csharp -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -o /tmp/csharp/ --model-name-mappings Tag=Label ``` -will rename the Tag schema to Label instead. +will rename the `Tag` schema to `Label` instead. +Not all generators support thess features yet. Please give it a try to confirm the behaviour and open an issue (ticket) to let us know which generators you would like to have this feature enabled and we'll prioritize accordingly. We also welcome PRs to add these features to generators. Related PRs for reference: #16209, #16234 (modelNameMappings), #16194, #16206 (nameMappings, parameterNameMappings). -(Not all generators support thess features yet. Please give it a try to confirm the behaviour and open an issue (ticket) to let us know which generators you would like to have this feature enabled and we'll prioritize accordingly.) - -Related PRs: #16209 (modelNameMappings), #16194, #16206 (nameMappings, parameterNameMappings) +NOTE: some generators use `baseName` (original name obtained direclty from OpenAPI spec, e.g. `shipping-date`) mustache tag in the templates so the mapping feature won't work. ## Schema Mapping diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java index c0ae1f4e8b7..0aef3f2ae27 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java @@ -507,6 +507,11 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo @Override public String toVarName(String name) { + // obtain the name from nameMapping directly if provided + if (nameMapping.containsKey(name)) { + return nameMapping.get(name); + } + // sanitize name name = sanitizeName(name); // FIXME parameter should not be assigned. Also declare it as "final" @@ -538,12 +543,22 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo @Override public String toParamName(String name) { + // obtain the name from parameterNameMapping directly if provided + if (parameterNameMapping.containsKey(name)) { + return parameterNameMapping.get(name); + } + // should be the same as variable name return toVarName(name); } @Override public String toModelName(String name) { + // obtain the name from modelNameMapping directly if provided + if (modelNameMapping.containsKey(name)) { + return modelNameMapping.get(name); + } + name = sanitizeName(name); // FIXME parameter should not be assigned. Also declare it as "final" if (!StringUtils.isEmpty(modelNamePrefix)) { diff --git a/samples/client/petstore/javascript-es6/.openapi-generator/FILES b/samples/client/petstore/javascript-es6/.openapi-generator/FILES index 9011fa6ed43..65b22f216a7 100644 --- a/samples/client/petstore/javascript-es6/.openapi-generator/FILES +++ b/samples/client/petstore/javascript-es6/.openapi-generator/FILES @@ -31,7 +31,7 @@ docs/Foo.md docs/FooGetDefaultResponse.md docs/FormatTest.md docs/HasOnlyReadOnly.md -docs/HealthCheckResult.md +docs/HealthCheckStatus.md docs/List.md docs/MapTest.md docs/MixedPropertiesAndAdditionalPropertiesClass.md @@ -97,7 +97,7 @@ src/model/Foo.js src/model/FooGetDefaultResponse.js src/model/FormatTest.js src/model/HasOnlyReadOnly.js -src/model/HealthCheckResult.js +src/model/HealthCheckStatus.js src/model/List.js src/model/MapTest.js src/model/MixedPropertiesAndAdditionalPropertiesClass.js diff --git a/samples/client/petstore/javascript-es6/README.md b/samples/client/petstore/javascript-es6/README.md index db2c6ef4a40..1f574fb0f35 100644 --- a/samples/client/petstore/javascript-es6/README.md +++ b/samples/client/petstore/javascript-es6/README.md @@ -190,7 +190,7 @@ Class | Method | HTTP request | Description - [OpenApiPetstore.FooGetDefaultResponse](docs/FooGetDefaultResponse.md) - [OpenApiPetstore.FormatTest](docs/FormatTest.md) - [OpenApiPetstore.HasOnlyReadOnly](docs/HasOnlyReadOnly.md) - - [OpenApiPetstore.HealthCheckResult](docs/HealthCheckResult.md) + - [OpenApiPetstore.HealthCheckStatus](docs/HealthCheckStatus.md) - [OpenApiPetstore.List](docs/List.md) - [OpenApiPetstore.MapTest](docs/MapTest.md) - [OpenApiPetstore.MixedPropertiesAndAdditionalPropertiesClass](docs/MixedPropertiesAndAdditionalPropertiesClass.md) diff --git a/samples/client/petstore/javascript-es6/docs/FakeApi.md b/samples/client/petstore/javascript-es6/docs/FakeApi.md index 5b76c0a7f50..a63ae962532 100644 --- a/samples/client/petstore/javascript-es6/docs/FakeApi.md +++ b/samples/client/petstore/javascript-es6/docs/FakeApi.md @@ -26,7 +26,7 @@ Method | HTTP request | Description ## fakeHealthGet -> HealthCheckResult fakeHealthGet() +> HealthCheckStatus fakeHealthGet() Health check endpoint @@ -51,7 +51,7 @@ This endpoint does not need any parameter. ### Return type -[**HealthCheckResult**](HealthCheckResult.md) +[**HealthCheckStatus**](HealthCheckStatus.md) ### Authorization @@ -78,7 +78,7 @@ let defaultClient = OpenApiPetstore.ApiClient.instance; let apiInstance = new OpenApiPetstore.FakeApi(); let pet = new OpenApiPetstore.Pet(); // Pet | Pet object that needs to be added to the store let opts = { - 'query1': "query1_example", // String | query parameter + 'queryOne': "queryOne_example", // String | query parameter 'header1': "header1_example" // String | header parameter }; apiInstance.fakeHttpSignatureTest(pet, opts, (error, data, response) => { @@ -96,7 +96,7 @@ apiInstance.fakeHttpSignatureTest(pet, opts, (error, data, response) => { Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- **pet** | [**Pet**](Pet.md)| Pet object that needs to be added to the store | - **query1** | **String**| query parameter | [optional] + **queryOne** | **String**| query parameter | [optional] **header1** | **String**| header parameter | [optional] ### Return type diff --git a/samples/client/petstore/javascript-es6/docs/HealthCheckResult.md b/samples/client/petstore/javascript-es6/docs/HealthCheckStatus.md similarity index 56% rename from samples/client/petstore/javascript-es6/docs/HealthCheckResult.md rename to samples/client/petstore/javascript-es6/docs/HealthCheckStatus.md index bbb74ac6a0a..1311bc15006 100644 --- a/samples/client/petstore/javascript-es6/docs/HealthCheckResult.md +++ b/samples/client/petstore/javascript-es6/docs/HealthCheckStatus.md @@ -1,9 +1,9 @@ -# OpenApiPetstore.HealthCheckResult +# OpenApiPetstore.HealthCheckStatus ## Properties Name | Type | Description | Notes ------------ | ------------- | ------------- | ------------- -**nullableMessage** | **String** | | [optional] +**nullable_field** | **String** | | [optional] diff --git a/samples/client/petstore/javascript-es6/src/api/FakeApi.js b/samples/client/petstore/javascript-es6/src/api/FakeApi.js index b80b6c6e81f..151b1fc60ef 100644 --- a/samples/client/petstore/javascript-es6/src/api/FakeApi.js +++ b/samples/client/petstore/javascript-es6/src/api/FakeApi.js @@ -16,7 +16,7 @@ import ApiClient from "../ApiClient"; import Client from '../model/Client'; import EnumClass from '../model/EnumClass'; import FileSchemaTestClass from '../model/FileSchemaTestClass'; -import HealthCheckResult from '../model/HealthCheckResult'; +import HealthCheckStatus from '../model/HealthCheckStatus'; import OuterComposite from '../model/OuterComposite'; import OuterObjectWithEnumProperty from '../model/OuterObjectWithEnumProperty'; import Pet from '../model/Pet'; @@ -45,14 +45,14 @@ export default class FakeApi { * Callback function to receive the result of the fakeHealthGet operation. * @callback module:api/FakeApi~fakeHealthGetCallback * @param {String} error Error message, if any. - * @param {module:model/HealthCheckResult} data The data returned by the service call. + * @param {module:model/HealthCheckStatus} data The data returned by the service call. * @param {String} response The complete HTTP response. */ /** * Health check endpoint * @param {module:api/FakeApi~fakeHealthGetCallback} callback The callback function, accepting three arguments: error, data, response - * data is of type: {@link module:model/HealthCheckResult} + * data is of type: {@link module:model/HealthCheckStatus} */ fakeHealthGet(callback) { let postBody = null; @@ -69,7 +69,7 @@ export default class FakeApi { let authNames = []; let contentTypes = []; let accepts = ['application/json']; - let returnType = HealthCheckResult; + let returnType = HealthCheckStatus; return this.apiClient.callApi( '/fake/health', 'GET', pathParams, queryParams, headerParams, formParams, postBody, @@ -89,7 +89,7 @@ export default class FakeApi { * test http signature authentication * @param {module:model/Pet} pet Pet object that needs to be added to the store * @param {Object} opts Optional parameters - * @param {String} [query1] query parameter + * @param {String} [queryOne] query parameter * @param {String} [header1] header parameter * @param {module:api/FakeApi~fakeHttpSignatureTestCallback} callback The callback function, accepting three arguments: error, data, response */ @@ -104,7 +104,7 @@ export default class FakeApi { let pathParams = { }; let queryParams = { - 'query_1': opts['query1'] + 'query_1': opts['queryOne'] }; let headerParams = { 'header_1': opts['header1'] diff --git a/samples/client/petstore/javascript-es6/src/index.js b/samples/client/petstore/javascript-es6/src/index.js index 2783bffde76..2466b0ebdab 100644 --- a/samples/client/petstore/javascript-es6/src/index.js +++ b/samples/client/petstore/javascript-es6/src/index.js @@ -38,7 +38,7 @@ import Foo from './model/Foo'; import FooGetDefaultResponse from './model/FooGetDefaultResponse'; import FormatTest from './model/FormatTest'; import HasOnlyReadOnly from './model/HasOnlyReadOnly'; -import HealthCheckResult from './model/HealthCheckResult'; +import HealthCheckStatus from './model/HealthCheckStatus'; import List from './model/List'; import MapTest from './model/MapTest'; import MixedPropertiesAndAdditionalPropertiesClass from './model/MixedPropertiesAndAdditionalPropertiesClass'; @@ -262,10 +262,10 @@ export { HasOnlyReadOnly, /** - * The HealthCheckResult model constructor. - * @property {module:model/HealthCheckResult} + * The HealthCheckStatus model constructor. + * @property {module:model/HealthCheckStatus} */ - HealthCheckResult, + HealthCheckStatus, /** * The List model constructor. diff --git a/samples/client/petstore/javascript-es6/src/model/HealthCheckResult.js b/samples/client/petstore/javascript-es6/src/model/HealthCheckStatus.js similarity index 75% rename from samples/client/petstore/javascript-es6/src/model/HealthCheckResult.js rename to samples/client/petstore/javascript-es6/src/model/HealthCheckStatus.js index 112fd778426..6299516cbbd 100644 --- a/samples/client/petstore/javascript-es6/src/model/HealthCheckResult.js +++ b/samples/client/petstore/javascript-es6/src/model/HealthCheckStatus.js @@ -14,19 +14,19 @@ import ApiClient from '../ApiClient'; /** - * The HealthCheckResult model module. - * @module model/HealthCheckResult + * The HealthCheckStatus model module. + * @module model/HealthCheckStatus * @version 1.0.0 */ -class HealthCheckResult { +class HealthCheckStatus { /** - * Constructs a new HealthCheckResult. + * Constructs a new HealthCheckStatus. * Just a string to inform instance is up and running. Make it nullable in hope to get it as pointer in generated model. - * @alias module:model/HealthCheckResult + * @alias module:model/HealthCheckStatus */ constructor() { - HealthCheckResult.initialize(this); + HealthCheckStatus.initialize(this); } /** @@ -38,15 +38,15 @@ class HealthCheckResult { } /** - * Constructs a HealthCheckResult from a plain JavaScript object, optionally creating a new instance. + * Constructs a HealthCheckStatus from a plain JavaScript object, optionally creating a new instance. * Copies all relevant properties from data to obj if supplied or a new instance if not. * @param {Object} data The plain JavaScript object bearing properties of interest. - * @param {module:model/HealthCheckResult} obj Optional instance to populate. - * @return {module:model/HealthCheckResult} The populated HealthCheckResult instance. + * @param {module:model/HealthCheckStatus} obj Optional instance to populate. + * @return {module:model/HealthCheckStatus} The populated HealthCheckStatus instance. */ static constructFromObject(data, obj) { if (data) { - obj = obj || new HealthCheckResult(); + obj = obj || new HealthCheckStatus(); if (data.hasOwnProperty('NullableMessage')) { obj['NullableMessage'] = ApiClient.convertToType(data['NullableMessage'], 'String'); @@ -56,9 +56,9 @@ class HealthCheckResult { } /** - * Validates the JSON data with respect to HealthCheckResult. + * Validates the JSON data with respect to HealthCheckStatus. * @param {Object} data The plain JavaScript object bearing properties of interest. - * @return {boolean} to indicate whether the JSON data is valid with respect to HealthCheckResult. + * @return {boolean} to indicate whether the JSON data is valid with respect to HealthCheckStatus. */ static validateJSON(data) { // ensure the json data is a string @@ -77,12 +77,12 @@ class HealthCheckResult { /** * @member {String} NullableMessage */ -HealthCheckResult.prototype['NullableMessage'] = undefined; +HealthCheckStatus.prototype['NullableMessage'] = undefined; -export default HealthCheckResult; +export default HealthCheckStatus; diff --git a/samples/client/petstore/javascript-es6/test/model/HealthCheckResult.spec.js b/samples/client/petstore/javascript-es6/test/model/HealthCheckStatus.spec.js similarity index 77% rename from samples/client/petstore/javascript-es6/test/model/HealthCheckResult.spec.js rename to samples/client/petstore/javascript-es6/test/model/HealthCheckStatus.spec.js index 25d9ecd929f..6eb9dccd9be 100644 --- a/samples/client/petstore/javascript-es6/test/model/HealthCheckResult.spec.js +++ b/samples/client/petstore/javascript-es6/test/model/HealthCheckStatus.spec.js @@ -28,7 +28,7 @@ var instance; beforeEach(function() { - instance = new OpenApiPetstore.HealthCheckResult(); + instance = new OpenApiPetstore.HealthCheckStatus(); }); var getProperty = function(object, getter, property) { @@ -47,16 +47,16 @@ object[property] = value; } - describe('HealthCheckResult', function() { - it('should create an instance of HealthCheckResult', function() { - // uncomment below and update the code to test HealthCheckResult - //var instance = new OpenApiPetstore.HealthCheckResult(); - //expect(instance).to.be.a(OpenApiPetstore.HealthCheckResult); + describe('HealthCheckStatus', function() { + it('should create an instance of HealthCheckStatus', function() { + // uncomment below and update the code to test HealthCheckStatus + //var instance = new OpenApiPetstore.HealthCheckStatus(); + //expect(instance).to.be.a(OpenApiPetstore.HealthCheckStatus); }); - it('should have the property nullableMessage (base name: "NullableMessage")', function() { - // uncomment below and update the code to test the property nullableMessage - //var instance = new OpenApiPetstore.HealthCheckResult(); + it('should have the property nullable_field (base name: "NullableMessage")', function() { + // uncomment below and update the code to test the property nullable_field + //var instance = new OpenApiPetstore.HealthCheckStatus(); //expect(instance).to.be(); });