mirror of
https://github.com/valitydev/openapi-generator.git
synced 2024-11-06 10:35:25 +00:00
Add better example code for R object (#2492)
* add better example for r object * better code format
This commit is contained in:
parent
e0caa6fd66
commit
3b017c59f1
@ -539,6 +539,7 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
|
||||
/**
|
||||
* Return the default value of the property
|
||||
*
|
||||
* @param p OpenAPI property object
|
||||
* @return string presentation of the default value of the property
|
||||
*/
|
||||
@ -598,4 +599,88 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
public String toModelTestFilename(String name) {
|
||||
return "test_" + toModelFilename(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
|
||||
Map<String, Object> objectMap = (Map<String, Object>) objs.get("operations");
|
||||
|
||||
HashMap<String, CodegenModel> modelMaps = new HashMap<String, CodegenModel>();
|
||||
for (Object o : allModels) {
|
||||
HashMap<String, Object> h = (HashMap<String, Object>) o;
|
||||
CodegenModel m = (CodegenModel) h.get("model");
|
||||
modelMaps.put(m.classname, m);
|
||||
}
|
||||
|
||||
List<CodegenOperation> operations = (List<CodegenOperation>) objectMap.get("operation");
|
||||
for (CodegenOperation operation : operations) {
|
||||
for (CodegenParameter cp : operation.allParams) {
|
||||
cp.vendorExtensions.put("x-r-example", constructExampleCode(cp, modelMaps));
|
||||
}
|
||||
}
|
||||
return objs;
|
||||
}
|
||||
|
||||
public String constructExampleCode(CodegenParameter codegenParameter, HashMap<String, CodegenModel> modelMaps) {
|
||||
if (codegenParameter.isListContainer) { // array
|
||||
return "list(" + constructExampleCode(codegenParameter.items, modelMaps) + ")";
|
||||
} else if (codegenParameter.isMapContainer) { // TODO: map
|
||||
return "TODO";
|
||||
} else if (languageSpecificPrimitives.contains(codegenParameter.dataType)) { // primitive type
|
||||
if ("character".equals(codegenParameter.dataType)) {
|
||||
return codegenParameter.example;
|
||||
} else {
|
||||
return codegenParameter.example;
|
||||
}
|
||||
} else { // model
|
||||
// look up the model
|
||||
if (modelMaps.containsKey(codegenParameter.dataType)) {
|
||||
return constructExampleCode(modelMaps.get(codegenParameter.dataType), modelMaps);
|
||||
} else {
|
||||
LOGGER.error("Error in constructing examples. Failed to look up the model " + codegenParameter.dataType);
|
||||
return "TODO";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String constructExampleCode(CodegenProperty codegenProperty, HashMap<String, CodegenModel> modelMaps) {
|
||||
if (codegenProperty.isListContainer) { // array
|
||||
return "list(" + constructExampleCode(codegenProperty.items, modelMaps) + ")";
|
||||
} else if (codegenProperty.isMapContainer) { // TODO: map
|
||||
return "TODO";
|
||||
} else if (languageSpecificPrimitives.contains(codegenProperty.dataType)) { // primitive type
|
||||
if ("character".equals(codegenProperty.dataType)) {
|
||||
if (StringUtils.isEmpty(codegenProperty.example)) {
|
||||
return "\"" + codegenProperty.example + "\"";
|
||||
} else {
|
||||
return "\"" + codegenProperty.name + "_example\"";
|
||||
}
|
||||
} else { // numeric
|
||||
if (StringUtils.isEmpty(codegenProperty.example)) {
|
||||
return codegenProperty.example;
|
||||
} else {
|
||||
return "123";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// look up the model
|
||||
if (modelMaps.containsKey(codegenProperty.dataType)) {
|
||||
return constructExampleCode(modelMaps.get(codegenProperty.dataType), modelMaps);
|
||||
} else {
|
||||
LOGGER.error("Error in constructing examples. Failed to look up the model " + codegenProperty.dataType);
|
||||
return "TODO";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String constructExampleCode(CodegenModel codegenModel, HashMap<String, CodegenModel> modelMaps) {
|
||||
String example;
|
||||
example = codegenModel.name + "$new(";
|
||||
List<String> propertyExamples = new ArrayList<>();
|
||||
for (CodegenProperty codegenProperty : codegenModel.vars) {
|
||||
propertyExamples.add(constructExampleCode(codegenProperty, modelMaps));
|
||||
}
|
||||
example += StringUtils.join(propertyExamples, ", ");
|
||||
example += ")";
|
||||
return example;
|
||||
}
|
||||
}
|
@ -22,7 +22,7 @@ Method | HTTP request | Description
|
||||
library({{{packageName}}})
|
||||
|
||||
{{#allParams}}
|
||||
var.{{{paramName}}} <- {{{example}}} # {{{dataType}}} | {{{description}}}
|
||||
var.{{{paramName}}} <- {{{vendorExtensions.x-r-example}}} # {{{dataType}}} | {{{description}}}
|
||||
{{/allParams}}
|
||||
|
||||
{{#summary}}
|
||||
|
@ -23,7 +23,7 @@ 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.body <- Pet$new(123, Category$new(123, "name_example"), "name_example", list("photoUrls_example"), list(Tag$new(123, "name_example")), "status_example") # Pet | Pet object that needs to be added to the store
|
||||
|
||||
#Add a new pet to the store
|
||||
api.instance <- PetApi$new()
|
||||
@ -105,7 +105,7 @@ Multiple status values can be provided with comma separated strings
|
||||
```R
|
||||
library(petstore)
|
||||
|
||||
var.status <- ['status_example'] # character | Status values that need to be considered for filter
|
||||
var.status <- list("status_example") # character | Status values that need to be considered for filter
|
||||
|
||||
#Finds Pets by status
|
||||
api.instance <- PetApi$new()
|
||||
@ -147,7 +147,7 @@ Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3
|
||||
```R
|
||||
library(petstore)
|
||||
|
||||
var.tags <- ['tags_example'] # character | Tags to filter by
|
||||
var.tags <- list("inner_example") # character | Tags to filter by
|
||||
|
||||
#Finds Pets by tags
|
||||
api.instance <- PetApi$new()
|
||||
@ -229,7 +229,7 @@ Update an existing pet
|
||||
```R
|
||||
library(petstore)
|
||||
|
||||
var.body <- Pet$new() # Pet | Pet object that needs to be added to the store
|
||||
var.body <- Pet$new(123, Category$new(123, "name_example"), "name_example", list("photoUrls_example"), list(Tag$new(123, "name_example")), "status_example") # Pet | Pet object that needs to be added to the store
|
||||
|
||||
#Update an existing pet
|
||||
api.instance <- PetApi$new()
|
||||
|
@ -136,7 +136,7 @@ Place an order for a pet
|
||||
```R
|
||||
library(petstore)
|
||||
|
||||
var.body <- Order$new() # Order | order placed for purchasing the pet
|
||||
var.body <- Order$new(123, 123, 123, "shipDate_example", "status_example", "complete_example") # Order | order placed for purchasing the pet
|
||||
|
||||
#Place an order for a pet
|
||||
api.instance <- StoreApi$new()
|
||||
|
@ -25,7 +25,7 @@ This can only be done by the logged in user.
|
||||
```R
|
||||
library(petstore)
|
||||
|
||||
var.body <- User$new() # User | Created user object
|
||||
var.body <- User$new(123, "username_example", "firstName_example", "lastName_example", "email_example", "password_example", "phone_example", 123) # User | Created user object
|
||||
|
||||
#Create user
|
||||
api.instance <- UserApi$new()
|
||||
@ -62,7 +62,7 @@ Creates list of users with given input array
|
||||
```R
|
||||
library(petstore)
|
||||
|
||||
var.body <- [array$new()] # User | List of user object
|
||||
var.body <- list(User$new(123, "username_example", "firstName_example", "lastName_example", "email_example", "password_example", "phone_example", 123)) # User | List of user object
|
||||
|
||||
#Creates list of users with given input array
|
||||
api.instance <- UserApi$new()
|
||||
@ -99,7 +99,7 @@ Creates list of users with given input array
|
||||
```R
|
||||
library(petstore)
|
||||
|
||||
var.body <- [array$new()] # User | List of user object
|
||||
var.body <- list(User$new(123, "username_example", "firstName_example", "lastName_example", "email_example", "password_example", "phone_example", 123)) # User | List of user object
|
||||
|
||||
#Creates list of users with given input array
|
||||
api.instance <- UserApi$new()
|
||||
@ -289,7 +289,7 @@ 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.body <- User$new(123, "username_example", "firstName_example", "lastName_example", "email_example", "password_example", "phone_example", 123) # User | Updated user object
|
||||
|
||||
#Updated user
|
||||
api.instance <- UserApi$new()
|
||||
|
Loading…
Reference in New Issue
Block a user