Java源码示例:org.openapitools.codegen.CodegenParameter
示例1
private CodegenVariable(CodegenVariable parent, CodegenParameter param, String testDataPath,
Map<String, CodegenModel> models) {
name = param.paramName;
dataFormat = param.dataFormat;
dataType = param.dataType;
enumName = param.enumName;
allowableValues = param.allowableValues;
isContainer = param.isContainer;
isListContainer = param.isListContainer;
isMapContainer = param.isMapContainer;
isPrimitiveType = param.isPrimitiveType;
minItems = param.minItems;
minimum = param.minimum;
maximum = param.maximum;
exclusiveMinimum = param.exclusiveMinimum;
exclusiveMaximum = param.exclusiveMaximum;
minLength = param.minLength;
maxLength = param.maxLength;
pattern = param.pattern;
setter = null;
varVendorExtensions = param.vendorExtensions;
init(parent, testDataPath, models);
items = param.items == null ? null : new CodegenVariable(this, param.items, null, models);
}
示例2
@Test(description = "test basic petstore operation with example body")
public void petstoreParameterExampleTest() {
final OpenAPI openAPI
= TestUtils.parseFlattenSpec("src/test/resources/2_0/petstore-bash.json");
final DefaultCodegen codegen = new BashClientCodegen();
codegen.setOpenAPI(openAPI);
final Operation addPetOperation
= openAPI.getPaths().get("/pet").getPost();
final CodegenOperation op
= codegen.fromOperation(
"/pet",
"POST",
addPetOperation,
null);
Assert.assertEquals(op.bodyParams.size(), 1);
CodegenParameter p = op.bodyParams.get(0);
Assert.assertTrue(p.vendorExtensions
.containsKey("x-codegen-body-example"));
Assert.assertEquals(p.description, "Pet object that needs to be added to the store");
}
示例3
@Override
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
objs = super.postProcessOperationsWithModels(objs, allModels);
@SuppressWarnings("unchecked")
Map<String, Object> objectMap = (Map<String, Object>) objs.get("operations");
@SuppressWarnings("unchecked")
List<CodegenOperation> operations = (List<CodegenOperation>) objectMap.get("operation");
List<Map<String, String>> imports = (List<Map<String, String>>) objs.get("imports");
if (imports == null)
return objs;
// override imports to only include packages for interface parameters
imports.clear();
boolean addedOptionalImport = false;
boolean addedTimeImport = false;
boolean addedOSImport = false;
boolean addedReflectImport = false;
for (CodegenOperation operation : operations) {
for (CodegenParameter param : operation.allParams) {
// import "os" if the operation uses files
if (!addedOSImport && "*os.File".equals(param.dataType)) {
imports.add(createMapping("import", "os"));
addedOSImport = true;
}
// import "time" if the operation has a required time parameter.
if (param.required) {
if (!addedTimeImport && "time.Time".equals(param.dataType)) {
imports.add(createMapping("import", "time"));
addedTimeImport = true;
}
}
}
}
return objs;
}
示例4
@Override
@SuppressWarnings("unchecked")
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
super.postProcessOperationsWithModels(objs, allModels);
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
if (operations != null) {
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation operation : ops) {
if (JVM_RETROFIT2.equals(getLibrary()) && StringUtils.isNotEmpty(operation.path) && operation.path.startsWith("/")) {
operation.path = operation.path.substring(1);
}
// set multipart against all relevant operations
if (operation.hasConsumes == Boolean.TRUE) {
if (isMultipartType(operation.consumes)) {
operation.isMultipart = Boolean.TRUE;
}
}
if (usesRetrofit2Library() && StringUtils.isNotEmpty(operation.path) && operation.path.startsWith("/")) {
operation.path = operation.path.substring(1);
}
// modify the data type of binary form parameters to a more friendly type for multiplatform builds
if (MULTIPLATFORM.equals(getLibrary()) && operation.allParams != null) {
for (CodegenParameter param : operation.allParams) {
if (param.dataFormat != null && param.dataFormat.equals("binary")) {
param.baseType = param.dataType = "io.ktor.client.request.forms.InputProvider";
}
}
}
}
}
return operations;
}
示例5
@Test(description = "test basic petstore operation with Bash extensions")
public void petstoreOperationTest() {
final OpenAPI openAPI
= TestUtils.parseFlattenSpec("src/test/resources/2_0/petstore-bash.json");
final DefaultCodegen codegen = new BashClientCodegen();
codegen.setOpenAPI(openAPI);
final Operation findPetsByStatusOperation
= openAPI.getPaths().get("/pet/findByStatus").getGet();
final CodegenOperation op
= codegen.fromOperation(
"/pet/findByStatus",
"GET",
findPetsByStatusOperation,
null);
Assert.assertTrue(
op.vendorExtensions.containsKey("x-code-samples"));
Assert.assertEquals(
op.vendorExtensions.get("x-bash-codegen-description"),
"Multiple status 'values' can be provided with comma separated strings");
Assert.assertEquals(op.allParams.size(), 1);
CodegenParameter p = op.allParams.get(0);
Assert.assertEquals(p.description, "Status values that need to be considered for filter");
}
示例6
@Test(description = "test example value for body parameter")
public void bodyParameterTest() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml");
final GoClientCodegen codegen = new GoClientCodegen();
codegen.setOpenAPI(openAPI);
final String path = "/fake";
final Operation p = openAPI.getPaths().get(path).getGet();
final CodegenOperation op = codegen.fromOperation(path, "post", p, null);
Assert.assertEquals(op.formParams.size(), 2);
CodegenParameter bp = op.formParams.get(0);
Assert.assertFalse(bp.isPrimitiveType);
}
示例7
@Test(description = "sets example value")
public void exampleValueTest() {
PhpClientCodegen clientCodegen = new PhpClientCodegen();
CodegenParameter p = new CodegenParameter();
p.baseType = "object";
clientCodegen.setParameterExampleValue(p);
Assert.assertEquals(p.example, "new \\stdClass");
}
示例8
@Override
public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
Map<String, Object> objsResult = super.postProcessOperations(objs);
Map<String, Object> operations = (Map<String, Object>) objsResult.get("operations");
if (operations != null && !models.isEmpty()) {
@SuppressWarnings("unchecked")
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
List<AgCodegenOperation> newOps = new ArrayList<AgCodegenOperation>();
for (CodegenOperation operation : ops) {
if (models.get(operation.baseName) == null) {
continue;
}
// Removes container from response
operation.returnType = operation.baseName;
AgCodegenOperation newOperation = new AgCodegenOperation(operation);
// Stores model properties to use them as constraints
populateModelAttributes(newOperation);
// Stores model relations to use them as constraints
for (final CodegenProperty prop : models.get(newOperation.baseName)) {
if (prop.complexType != null && models.keySet().contains(prop.complexType)) {
AgCodegenOperation relation = new AgCodegenOperation();
relation.returnType = relation.baseName = prop.complexType;
relation.bodyParam = new CodegenParameter();
relation.bodyParam.paramName = prop.baseName;
populateModelAttributes(relation);
populateRelations(newOperation, relation);
}
}
newOps.add(newOperation);
}
operations.put("operation", newOps);
}
return objsResult;
}
示例9
private void populateModelAttributes(AgCodegenOperation operation) {
if (models.get(operation.baseName) != null) {
for (CodegenProperty prop : models.get(operation.baseName)) {
// Selects plain attributes only
if ((prop.complexType == null || !models.keySet().contains(prop.complexType))
&& !"id".equalsIgnoreCase(prop.baseName)) {
CodegenParameter codegenParam = new CodegenParameter();
codegenParam.paramName = prop.baseName;
codegenParam.hasMore = true;
// checks if there is queryParam with the same name as model attribute
if (operation.queryParams.stream().anyMatch(p -> codegenParam.paramName.equalsIgnoreCase(p.paramName))) {
operation.hasCompoundId = codegenParam.isQueryParam = true;
// removes model attribute related to queryParam from params list
operation.queryParams
= operation.queryParams
.stream()
.filter(p -> !p.paramName.equalsIgnoreCase(codegenParam.paramName))
.collect(Collectors.toList());
}
operation.modelAttributes.add(codegenParam);
}
}
if (!operation.modelAttributes.isEmpty()) {
operation.modelAttributes.get(operation.modelAttributes.size() - 1).hasMore = false;
}
}
}
示例10
public static List<CodegenParameter> extend(List<CodegenParameter> parameters) {
// TODO: does it ever happen?
if (parameters == null) {
return null;
}
CodegenParameter sort = null;
CodegenParameter dir = null;
for (CodegenParameter p : parameters) {
if ("dir".equals(p.baseName)) {
// do not add "dir" value to the builder directly... track it here and combine with "sort" later
dir = p;
} else {
if ("sort".equals(p.baseName)) {
sort = p;
}
p.vendorExtensions.put(PARAM_ADD_TO_REQUEST_METHOD, addToRequestMethods.getOrDefault(p.baseName, p.baseName));
}
}
// combine sort and dir
if (sort != null && dir != null) {
sort.vendorExtensions.put(PARAM_REQUEST_METHOD_SECOND_ARG, ", dir");
}
return parameters;
}
示例11
@Override
public void postProcessParameter(CodegenParameter parameter) {
super.postProcessParameter(parameter);
// parameter.dataType = addModelPrefix(parameter.dataType);
}
示例12
@Override
public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> objs, List<Object> allModels) {
objs = super.postProcessOperationsWithModels(objs, allModels);
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
Set<String> modelImports = new HashSet<>();
Set<String> fullImports = new HashSet<>();
for (CodegenOperation op : operationList) {
op.httpMethod = op.httpMethod.toLowerCase(Locale.ROOT);
boolean isJson = true; //default to JSON
boolean isForm = false;
boolean isMultipart = false;
if (op.consumes != null) {
for (Map<String, String> consume : op.consumes) {
if (consume.containsKey("mediaType")) {
String type = consume.get("mediaType");
isJson = type.equalsIgnoreCase("application/json");
isForm = type.equalsIgnoreCase("application/x-www-form-urlencoded");
isMultipart = type.equalsIgnoreCase("multipart/form-data");
break;
}
}
}
for (CodegenParameter param : op.bodyParams) {
if (param.baseType != null && param.baseType.equalsIgnoreCase("Uint8List") && isMultipart) {
param.baseType = "MultipartFile";
param.dataType = "MultipartFile";
}
}
op.vendorExtensions.put("x-is-json", isJson);
op.vendorExtensions.put("x-is-form", isForm);
op.vendorExtensions.put("x-is-multipart", isMultipart);
if (op.getHasFormParams()) {
fullImports.add("package:" + pubName + "/api_util.dart");
}
Set<String> imports = new HashSet<>();
for (String item : op.imports) {
if (!modelToIgnore.contains(item.toLowerCase(Locale.ROOT))) {
imports.add(underscore(item));
} else if (item.equalsIgnoreCase("Uint8List")) {
fullImports.add("dart:typed_data");
}
}
modelImports.addAll(imports);
op.imports = imports;
}
objs.put("modelImports", modelImports);
objs.put("fullImports", fullImports);
return objs;
}