From 9695090d9b5e0b9e078f2f5749528f56061a4aaa Mon Sep 17 00:00:00 2001 From: William Cheng Date: Mon, 25 Feb 2019 19:09:26 +0800 Subject: [PATCH] [R] Fix NPE issue due to default value using example value (#2231) * fix NPE issue due to default value using example value * update r petstore sample --- .../codegen/languages/RClientCodegen.java | 35 ---------------- samples/client/petstore/R/NAMESPACE | 2 + samples/client/petstore/R/R/pet_api.R | 20 +++++----- samples/client/petstore/R/R/store_api.R | 10 ++--- samples/client/petstore/R/R/user_api.R | 40 +++++++++---------- samples/client/petstore/R/README.md | 2 + samples/client/petstore/R/docs/Pet.md | 2 +- samples/client/petstore/R/docs/PetApi.md | 16 ++++---- samples/client/petstore/R/docs/StoreApi.md | 10 ++--- samples/client/petstore/R/docs/UserApi.md | 40 +++++++++---------- 10 files changed, 73 insertions(+), 104 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java index 636abcb38ee..5f7f000868a 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RClientCodegen.java @@ -551,14 +551,6 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig { else return "TRUE"; } - // include fallback to example, default defined as server only - // example is not defined as server only - if (p.getExample() != null) { - if (Boolean.valueOf(p.getExample().toString()) == false) - return "FALSE"; - else - return "TRUE"; - } } else if (ModelUtils.isDateSchema(p)) { // TODO } else if (ModelUtils.isDateTimeSchema(p)) { @@ -567,24 +559,10 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig { if (p.getDefault() != null) { return p.getDefault().toString(); } - // default numbers are not yet returned by v2 spec openAPI results - // https://github.com/swagger-api/swagger-parser/issues/971 - // include fallback to example, default defined as server only - // example is not defined as server only - if (p.getExample() != null) { - return p.getExample().toString(); - } } else if (ModelUtils.isIntegerSchema(p)) { if (p.getDefault() != null) { return p.getDefault().toString(); } - // default integers are not yet returned by v2 spec openAPI results - // https://github.com/swagger-api/swagger-parser/issues/971 - // include fallback to example, default defined as server only - // example is not defined as server only - if (p.getExample() != null) { - return p.getExample().toString(); - } } else if (ModelUtils.isStringSchema(p)) { if (p.getDefault() != null) { if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find()) @@ -592,23 +570,10 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig { else return "'" + p.getDefault() + "'"; } - // include fallback to example, default defined as server only - // example is not defined as server only - if (p.getExample() != null) { - if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getExample()).find()) - return "'''" + p.getExample() + "'''"; - else - return "'" + p.getExample() + "'"; - } } else if (ModelUtils.isArraySchema(p)) { if (p.getDefault() != null) { return p.getDefault().toString(); } - // include fallback to example, default defined as server only - // example is not defined as server only - if (p.getExample() != null) { - return p.getExample().toString(); - } } return null; diff --git a/samples/client/petstore/R/NAMESPACE b/samples/client/petstore/R/NAMESPACE index 42af616b975..c38dd450a16 100644 --- a/samples/client/petstore/R/NAMESPACE +++ b/samples/client/petstore/R/NAMESPACE @@ -7,6 +7,8 @@ export(ApiResponse) # Models export(Category) +export(InlineObject) +export(InlineObject1) export(ModelApiResponse) export(Order) export(Pet) diff --git a/samples/client/petstore/R/R/pet_api.R b/samples/client/petstore/R/R/pet_api.R index 224ee31243a..b84d12ffc5f 100644 --- a/samples/client/petstore/R/R/pet_api.R +++ b/samples/client/petstore/R/R/pet_api.R @@ -56,17 +56,17 @@ PetApi <- R6::R6Class( self$apiClient <- ApiClient$new() } }, - AddPet = function(body, ...){ + AddPet = function(pet, ...){ args <- list(...) queryParams <- list() headerParams <- c() - if (missing(`body`)) { - stop("Missing required parameter `body`.") + if (missing(`pet`)) { + stop("Missing required parameter `pet`.") } - if (!missing(`body`)) { - body <- `body`$toJSONString() + if (!missing(`pet`)) { + body <- `pet`$toJSONString() } else { body <- NULL } @@ -223,17 +223,17 @@ PetApi <- R6::R6Class( } }, - UpdatePet = function(body, ...){ + UpdatePet = function(pet, ...){ args <- list(...) queryParams <- list() headerParams <- c() - if (missing(`body`)) { - stop("Missing required parameter `body`.") + if (missing(`pet`)) { + stop("Missing required parameter `pet`.") } - if (!missing(`body`)) { - body <- `body`$toJSONString() + if (!missing(`pet`)) { + body <- `pet`$toJSONString() } else { body <- NULL } diff --git a/samples/client/petstore/R/R/store_api.R b/samples/client/petstore/R/R/store_api.R index 9c6db7193a8..8c1ad82985c 100644 --- a/samples/client/petstore/R/R/store_api.R +++ b/samples/client/petstore/R/R/store_api.R @@ -133,17 +133,17 @@ StoreApi <- R6::R6Class( } }, - PlaceOrder = function(body, ...){ + PlaceOrder = function(order, ...){ args <- list(...) queryParams <- list() headerParams <- c() - if (missing(`body`)) { - stop("Missing required parameter `body`.") + if (missing(`order`)) { + stop("Missing required parameter `order`.") } - if (!missing(`body`)) { - body <- `body`$toJSONString() + if (!missing(`order`)) { + body <- `order`$toJSONString() } else { body <- NULL } diff --git a/samples/client/petstore/R/R/user_api.R b/samples/client/petstore/R/R/user_api.R index 0c676509ed9..cbc85ed69dc 100644 --- a/samples/client/petstore/R/R/user_api.R +++ b/samples/client/petstore/R/R/user_api.R @@ -56,17 +56,17 @@ UserApi <- R6::R6Class( self$apiClient <- ApiClient$new() } }, - CreateUser = function(body, ...){ + CreateUser = function(user, ...){ args <- list(...) queryParams <- list() headerParams <- c() - if (missing(`body`)) { - stop("Missing required parameter `body`.") + if (missing(`user`)) { + stop("Missing required parameter `user`.") } - if (!missing(`body`)) { - body <- `body`$toJSONString() + if (!missing(`user`)) { + body <- `user`$toJSONString() } else { body <- NULL } @@ -89,17 +89,17 @@ UserApi <- R6::R6Class( } }, - CreateUsersWithArrayInput = function(body, ...){ + CreateUsersWithArrayInput = function(user, ...){ args <- list(...) queryParams <- list() headerParams <- c() - if (missing(`body`)) { - stop("Missing required parameter `body`.") + if (missing(`user`)) { + stop("Missing required parameter `user`.") } - if (!missing(`body`)) { - body <- `body`$toJSONString() + if (!missing(`user`)) { + body <- `user`$toJSONString() } else { body <- NULL } @@ -122,17 +122,17 @@ UserApi <- R6::R6Class( } }, - CreateUsersWithListInput = function(body, ...){ + CreateUsersWithListInput = function(user, ...){ args <- list(...) queryParams <- list() headerParams <- c() - if (missing(`body`)) { - stop("Missing required parameter `body`.") + if (missing(`user`)) { + stop("Missing required parameter `user`.") } - if (!missing(`body`)) { - body <- `body`$toJSONString() + if (!missing(`user`)) { + body <- `user`$toJSONString() } else { body <- NULL } @@ -275,7 +275,7 @@ UserApi <- R6::R6Class( } }, - UpdateUser = function(username, body, ...){ + UpdateUser = function(username, user, ...){ args <- list(...) queryParams <- list() headerParams <- c() @@ -284,12 +284,12 @@ UserApi <- R6::R6Class( stop("Missing required parameter `username`.") } - if (missing(`body`)) { - stop("Missing required parameter `body`.") + if (missing(`user`)) { + stop("Missing required parameter `user`.") } - if (!missing(`body`)) { - body <- `body`$toJSONString() + if (!missing(`user`)) { + body <- `user`$toJSONString() } else { body <- NULL } diff --git a/samples/client/petstore/R/README.md b/samples/client/petstore/R/README.md index bdff66e39bc..dd1671cf8a0 100644 --- a/samples/client/petstore/R/README.md +++ b/samples/client/petstore/R/README.md @@ -81,6 +81,8 @@ Class | Method | HTTP request | Description ## Documentation for Models - [Category](docs/Category.md) + - [InlineObject](docs/InlineObject.md) + - [InlineObject1](docs/InlineObject1.md) - [ModelApiResponse](docs/ModelApiResponse.md) - [Order](docs/Order.md) - [Pet](docs/Pet.md) diff --git a/samples/client/petstore/R/docs/Pet.md b/samples/client/petstore/R/docs/Pet.md index 33478b28407..296dfdeea43 100644 --- a/samples/client/petstore/R/docs/Pet.md +++ b/samples/client/petstore/R/docs/Pet.md @@ -5,7 +5,7 @@ Name | Type | Description | Notes ------------ | ------------- | ------------- | ------------- **id** | **integer** | | [optional] **category** | [**Category**](Category.md) | | [optional] -**name** | **character** | | [default to 'doggie'] +**name** | **character** | | **photoUrls** | **character** | | **tags** | [**Tag**](Tag.md) | | [optional] **status** | **character** | pet status in the store | [optional] diff --git a/samples/client/petstore/R/docs/PetApi.md b/samples/client/petstore/R/docs/PetApi.md index 82e44deaa97..3b313d95737 100644 --- a/samples/client/petstore/R/docs/PetApi.md +++ b/samples/client/petstore/R/docs/PetApi.md @@ -15,7 +15,7 @@ Method | HTTP request | Description # **AddPet** -> AddPet(body) +> AddPet(pet) Add a new pet to the store @@ -23,20 +23,20 @@ Add a new pet to the store ```R library(petstore) -var.body <- Pet$new() # Pet | Pet object that needs to be added to the store +var.pet <- Pet$new() # Pet | Pet object that needs to be added to the store #Add a new pet to the store api.instance <- PetApi$new() # Configure OAuth2 access token for authorization: petstore_auth api.instance$apiClient$accessToken <- 'TODO_YOUR_ACCESS_TOKEN'; -api.instance$AddPet(var.body) +api.instance$AddPet(var.pet) ``` ### Parameters Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **body** | [**Pet**](Pet.md)| Pet object that needs to be added to the store | + **pet** | [**Pet**](Pet.md)| Pet object that needs to be added to the store | ### Return type @@ -221,7 +221,7 @@ Name | Type | Description | Notes # **UpdatePet** -> UpdatePet(body) +> UpdatePet(pet) Update an existing pet @@ -229,20 +229,20 @@ Update an existing pet ```R library(petstore) -var.body <- Pet$new() # Pet | Pet object that needs to be added to the store +var.pet <- Pet$new() # Pet | Pet object that needs to be added to the store #Update an existing pet api.instance <- PetApi$new() # Configure OAuth2 access token for authorization: petstore_auth api.instance$apiClient$accessToken <- 'TODO_YOUR_ACCESS_TOKEN'; -api.instance$UpdatePet(var.body) +api.instance$UpdatePet(var.pet) ``` ### Parameters Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **body** | [**Pet**](Pet.md)| Pet object that needs to be added to the store | + **pet** | [**Pet**](Pet.md)| Pet object that needs to be added to the store | ### Return type diff --git a/samples/client/petstore/R/docs/StoreApi.md b/samples/client/petstore/R/docs/StoreApi.md index 395b10ebd10..ae038a43c81 100644 --- a/samples/client/petstore/R/docs/StoreApi.md +++ b/samples/client/petstore/R/docs/StoreApi.md @@ -128,7 +128,7 @@ No authorization required # **PlaceOrder** -> Order PlaceOrder(body) +> Order PlaceOrder(order) Place an order for a pet @@ -136,11 +136,11 @@ Place an order for a pet ```R library(petstore) -var.body <- Order$new() # Order | order placed for purchasing the pet +var.order <- Order$new() # Order | order placed for purchasing the pet #Place an order for a pet api.instance <- StoreApi$new() -result <- api.instance$PlaceOrder(var.body) +result <- api.instance$PlaceOrder(var.order) dput(result) ``` @@ -148,7 +148,7 @@ dput(result) Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **body** | [**Order**](Order.md)| order placed for purchasing the pet | + **order** | [**Order**](Order.md)| order placed for purchasing the pet | ### Return type @@ -160,7 +160,7 @@ No authorization required ### HTTP request headers - - **Content-Type**: Not defined + - **Content-Type**: application/json - **Accept**: application/xml, application/json diff --git a/samples/client/petstore/R/docs/UserApi.md b/samples/client/petstore/R/docs/UserApi.md index a63c8f691e9..db5d0e9bc44 100644 --- a/samples/client/petstore/R/docs/UserApi.md +++ b/samples/client/petstore/R/docs/UserApi.md @@ -15,7 +15,7 @@ Method | HTTP request | Description # **CreateUser** -> CreateUser(body) +> CreateUser(user) Create user @@ -25,18 +25,18 @@ This can only be done by the logged in user. ```R library(petstore) -var.body <- User$new() # User | Created user object +var.user <- User$new() # User | Created user object #Create user api.instance <- UserApi$new() -api.instance$CreateUser(var.body) +api.instance$CreateUser(var.user) ``` ### Parameters Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **body** | [**User**](User.md)| Created user object | + **user** | [**User**](User.md)| Created user object | ### Return type @@ -48,13 +48,13 @@ No authorization required ### HTTP request headers - - **Content-Type**: Not defined + - **Content-Type**: application/json - **Accept**: Not defined # **CreateUsersWithArrayInput** -> CreateUsersWithArrayInput(body) +> CreateUsersWithArrayInput(user) Creates list of users with given input array @@ -62,18 +62,18 @@ Creates list of users with given input array ```R library(petstore) -var.body <- [array$new()] # User | List of user object +var.user <- [array$new()] # User | List of user object #Creates list of users with given input array api.instance <- UserApi$new() -api.instance$CreateUsersWithArrayInput(var.body) +api.instance$CreateUsersWithArrayInput(var.user) ``` ### Parameters Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **body** | [**User**](array.md)| List of user object | + **user** | [**User**](array.md)| List of user object | ### Return type @@ -85,13 +85,13 @@ No authorization required ### HTTP request headers - - **Content-Type**: Not defined + - **Content-Type**: application/json - **Accept**: Not defined # **CreateUsersWithListInput** -> CreateUsersWithListInput(body) +> CreateUsersWithListInput(user) Creates list of users with given input array @@ -99,18 +99,18 @@ Creates list of users with given input array ```R library(petstore) -var.body <- [array$new()] # User | List of user object +var.user <- [array$new()] # User | List of user object #Creates list of users with given input array api.instance <- UserApi$new() -api.instance$CreateUsersWithListInput(var.body) +api.instance$CreateUsersWithListInput(var.user) ``` ### Parameters Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- - **body** | [**User**](array.md)| List of user object | + **user** | [**User**](array.md)| List of user object | ### Return type @@ -122,7 +122,7 @@ No authorization required ### HTTP request headers - - **Content-Type**: Not defined + - **Content-Type**: application/json - **Accept**: Not defined @@ -278,7 +278,7 @@ No authorization required # **UpdateUser** -> UpdateUser(username, body) +> UpdateUser(username, user) Updated user @@ -289,11 +289,11 @@ This can only be done by the logged in user. library(petstore) var.username <- 'username_example' # character | name that need to be deleted -var.body <- User$new() # User | Updated user object +var.user <- User$new() # User | Updated user object #Updated user api.instance <- UserApi$new() -api.instance$UpdateUser(var.username, var.body) +api.instance$UpdateUser(var.username, var.user) ``` ### Parameters @@ -301,7 +301,7 @@ api.instance$UpdateUser(var.username, var.body) Name | Type | Description | Notes ------------- | ------------- | ------------- | ------------- **username** | **character**| name that need to be deleted | - **body** | [**User**](User.md)| Updated user object | + **user** | [**User**](User.md)| Updated user object | ### Return type @@ -313,7 +313,7 @@ No authorization required ### HTTP request headers - - **Content-Type**: Not defined + - **Content-Type**: application/json - **Accept**: Not defined