use of io.swagger.codegen.CodegenProperty in project okta-sdk-java by okta.
the class AbstractOktaJavaClientCodegen method fromModel.
@Override
public CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions) {
CodegenModel codegenModel = super.fromModel(name, model, allDefinitions);
// super add these imports, and we don't want that dependency
codegenModel.imports.remove("ApiModel");
if (model.getVendorExtensions().containsKey("x-baseType")) {
String baseType = (String) model.getVendorExtensions().get("x-baseType");
codegenModel.vendorExtensions.put("baseType", toModelName(baseType));
codegenModel.imports.add(toModelName(baseType));
}
Collection<CodegenOperation> operations = (Collection<CodegenOperation>) codegenModel.vendorExtensions.get("operations");
if (operations != null) {
operations.forEach(op -> {
if (op.returnType != null) {
codegenModel.imports.add(op.returnType);
}
if (op.allParams != null) {
op.allParams.stream().filter(param -> needToImport(param.dataType)).forEach(param -> codegenModel.imports.add(param.dataType));
}
});
}
// force alias == false (likely only relevant for Lists, but something changed in swagger 2.2.3 to require this)
codegenModel.isAlias = false;
String parent = (String) model.getVendorExtensions().get("x-okta-parent");
if (StringUtils.isNotEmpty(parent)) {
codegenModel.parent = toApiName(parent.substring(parent.lastIndexOf("/")));
// figure out the resourceClass if this model has a parent
String discriminatorRoot = getRootDiscriminator(name);
if (discriminatorRoot != null) {
model.getVendorExtensions().put("discriminatorRoot", discriminatorRoot);
}
}
// We use '$ref' attributes with siblings, which isn't valid JSON schema (or swagger), so we need process
// additional attributes from the raw schema
Map<String, Object> modelDef = getRawSwaggerDefinition(name);
codegenModel.vars.forEach(codegenProperty -> {
Map<String, Object> rawPropertyMap = getRawSwaggerProperty(modelDef, codegenProperty.baseName);
codegenProperty.isReadOnly = Boolean.TRUE.equals(rawPropertyMap.get("readOnly"));
});
return codegenModel;
}
use of io.swagger.codegen.CodegenProperty in project okta-sdk-java by okta.
the class AbstractOktaJavaClientCodegen method handleOktaLinkedOperations.
private void handleOktaLinkedOperations(Swagger swagger) {
// we want to move any operations defined by the 'x-okta-operations' or 'x-okta-crud' vendor extension to the model
Map<String, Model> modelMap = swagger.getDefinitions().entrySet().stream().filter(e -> e.getValue().getVendorExtensions().containsKey("x-okta-operations") || e.getValue().getVendorExtensions().containsKey("x-okta-crud")).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
modelMap.forEach((k, model) -> {
List<ObjectNode> linkNodes = new ArrayList<>();
addAllIfNotNull(linkNodes, (List<ObjectNode>) model.getVendorExtensions().get("x-okta-operations"));
addAllIfNotNull(linkNodes, (List<ObjectNode>) model.getVendorExtensions().get("x-okta-crud"));
Map<String, CodegenOperation> operationMap = new HashMap<>();
linkNodes.forEach(n -> {
String operationId = n.get("operationId").textValue();
// find the swagger path operation
swagger.getPaths().forEach((pathName, path) -> {
Optional<Map.Entry<HttpMethod, Operation>> operationEntry = path.getOperationMap().entrySet().stream().filter(e -> e.getValue().getOperationId().equals(operationId)).findFirst();
if (operationEntry.isPresent()) {
Operation operation = operationEntry.get().getValue();
CodegenOperation cgOperation = fromOperation(pathName, operationEntry.get().getKey().name().toLowerCase(), operation, swagger.getDefinitions(), swagger);
boolean canLinkMethod = true;
JsonNode aliasNode = n.get("alias");
if (aliasNode != null) {
String alias = aliasNode.textValue();
cgOperation.vendorExtensions.put("alias", alias);
if ("update".equals(alias)) {
model.getVendorExtensions().put("saveable", true);
} else if ("delete".equals(alias)) {
model.getVendorExtensions().put("deletable", true);
cgOperation.vendorExtensions.put("selfDelete", true);
} else if ("read".equals(alias) || "create".equals(alias)) {
canLinkMethod = false;
}
}
// we do NOT link read or create methods, those need to be on the parent object
if (canLinkMethod) {
// now any params that match the models we need to use the model value directly
// for example if the path contained {id} we would call getId() instead
Map<String, String> argMap = createArgMap(n);
List<CodegenParameter> cgOtherPathParamList = new ArrayList<>();
List<CodegenParameter> cgParamAllList = new ArrayList<>();
List<CodegenParameter> cgParamModelList = new ArrayList<>();
cgOperation.pathParams.forEach(param -> {
if (argMap.containsKey(param.paramName)) {
String paramName = argMap.get(param.paramName);
cgParamModelList.add(param);
if (model.getProperties() != null) {
CodegenProperty cgProperty = fromProperty(paramName, model.getProperties().get(paramName));
param.vendorExtensions.put("fromModel", cgProperty);
} else {
System.err.println("Model '" + model.getTitle() + "' has no properties");
}
} else {
cgOtherPathParamList.add(param);
}
});
// remove the body param if the body is the object itself
for (Iterator<CodegenParameter> iter = cgOperation.bodyParams.iterator(); iter.hasNext(); ) {
CodegenParameter bodyParam = iter.next();
if (argMap.containsKey(bodyParam.paramName)) {
cgOperation.vendorExtensions.put("bodyIsSelf", true);
iter.remove();
}
}
// do not add the parrent path params to the list (they will be parsed from the href)
SortedSet<String> pathParents = parentPathParams(n);
cgOtherPathParamList.forEach(param -> {
if (!pathParents.contains(param.paramName)) {
cgParamAllList.add(param);
}
});
if (!pathParents.isEmpty()) {
cgOperation.vendorExtensions.put("hasPathParents", true);
cgOperation.vendorExtensions.put("pathParents", pathParents);
}
cgParamAllList.addAll(cgOperation.queryParams);
cgParamAllList.addAll(cgOperation.bodyParams);
// set all params to have more
cgParamAllList.forEach(param -> param.hasMore = true);
// then grab the last one and mark it as the last
if (!cgParamAllList.isEmpty()) {
CodegenParameter param = cgParamAllList.get(cgParamAllList.size() - 1);
param.hasMore = false;
}
cgOperation.vendorExtensions.put("allParams", cgParamAllList);
cgOperation.vendorExtensions.put("fromModelPathParams", cgParamModelList);
addOptionalExtension(cgOperation, cgParamAllList);
operationMap.put(cgOperation.operationId, cgOperation);
// mark the operation as moved so we do NOT add it to the client
operation.getVendorExtensions().put("moved", true);
}
}
});
});
model.getVendorExtensions().put("operations", operationMap.values());
});
}
Aggregations