Java源码示例:io.apicurio.datamodels.openapi.models.OasParameter
示例1
@Override
protected Optional<Violation> validateRequestSchema(String operationId, String path, OasOperation operation) {
for (final OasParameter parameter : OasModelHelper.getParameters(operation)) {
if (!OasModelHelper.isBody(parameter)) {
continue;
}
final OasSchema schema = (OasSchema) parameter.schema;
if (OasModelHelper.schemaIsNotSpecified(schema)) {
final String message = "Operation " + operationId + " " + path
+ " does not provide a schema for the body parameter";
return Optional.of(new Violation.Builder()//
.property("")//
.error("missing-request-schema")//
.message(message)//
.build());
}
}
return Optional.empty();
}
示例2
public static Optional<OasSchema> getRequestBodySchema(Oas20Document openApiDoc, Oas20Operation operation) {
if (operation.parameters == null) {
return Optional.empty();
}
final List<OasParameter> operationParameters = operation.parameters;
Optional<OasParameter> body = operationParameters.stream()
.filter(p -> "body".equals(p.in) && p.schema != null)
.findFirst();
return body.map(oasParameter -> (OasSchema) oasParameter.schema);
}
示例3
/**
* Find parameter that is defined to live in body.
*/
static Optional<OasParameter> findBodyParameter(Oas20Operation operation) {
if (operation.parameters == null) {
return Optional.empty();
}
final List<OasParameter> operationParameters = operation.parameters;
return operationParameters.stream()
.filter(p -> "body".equals(p.in) && p.schema != null)
.findFirst();
}
示例4
static Optional<DataShapeGenerator.NameAndSchema> findBodySchema(Oas20Operation operation) {
Optional<OasParameter> maybeBody = Oas20ModelHelper.findBodyParameter(operation);
if (maybeBody.isPresent()) {
OasParameter body = maybeBody.get();
String name = ofNullable(body.getName()).orElse(body.description);
return Optional.of(new DataShapeGenerator.NameAndSchema(name, (OasSchema) body.schema));
}
return empty();
}
示例5
/**
* Checks if we should generate a configuration property for given global parameter.
* @param parameter the parameter to check.
* @return true if parameter should generate a configuration property.
*/
protected boolean shouldCreateProperty(OasParameter parameter) {
if (OasModelHelper.isReferenceType(parameter) || OasModelHelper.isBody(parameter)) {
// Reference parameters are not supported, body parameters are
// handled in createShape* methods
return false;
}
return OasModelHelper.isSerializable(parameter);
}
示例6
/**
* Creates a configuration property from given parameter. OpenAPI version specific information like type, default values and enum
* list are extracted by version specific subclass implementations.
* @param parameter the global parameter on the specification.
* @param type the parameter type (array, object, etc.)
* @param javaType the corresponding java type.
* @param defaultValue optional default value.
* @param enums optional list of allowed values as enumeration.
* @return generated configuration property that gets added to the connector builder.
*/
public static ConfigurationProperty createPropertyFromParameter(final OasParameter parameter,
final String type, final String javaType,
final Object defaultValue, List<String> enums) {
final String name = trimToNull(parameter.name);
final String description = trimToNull(parameter.description);
final boolean required = Boolean.TRUE.equals(parameter.required);
final ConfigurationProperty.Builder propertyBuilder = new ConfigurationProperty.Builder()
.kind("property")
.displayName(name)
.description(description)
.group("producer")
.required(required)
.componentProperty(false)
.deprecated(false)
.secret(false);
if (defaultValue != null) {
propertyBuilder.defaultValue(String.valueOf(defaultValue));
}
propertyBuilder.type(type).javaType(javaType);
if (enums != null) {
propertyBuilder.addAllEnum(createEnums(enums));
}
return propertyBuilder.build();
}
示例7
public static List<OasParameter> getParameters(final OasOperation operation) {
if (operation == null || operation.getParameters() == null) {
return new ArrayList<>();
}
return operation.getParameters();
}
示例8
public static List<OasParameter> getParameters(final OasPathItem pathItem) {
if (pathItem == null || pathItem.getParameters() == null) {
return new ArrayList<>();
}
return pathItem.getParameters();
}
示例9
/**
* @see io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter#visitParameter(io.apicurio.datamodels.core.models.common.Parameter)
*/
@Override
public void visitParameter(Parameter node) {
// Skip processing of the parameter if it is defined at the path level.
if (!this._processPathItemParams && this.isPathItem(node.parent())) {
return;
}
OasParameter param = (OasParameter) node;
CodegenJavaArgument cgArgument = new CodegenJavaArgument();
cgArgument.setName(param.name);
cgArgument.setIn(param.in);
cgArgument.setRequired(true);
this._currentMethod.getArguments().add(cgArgument);
this._currentArgument = cgArgument;
if (param.required != null) {
cgArgument.setRequired(param.required);
}
if (param.ownerDocument().getDocumentType() == DocumentType.openapi2) {
this.visit20Parameter((Oas20Parameter) param);
}
if (param.ownerDocument().getDocumentType() == DocumentType.openapi3) {
this.visit30Parameter((Oas30Parameter) param);
}
}
示例10
/**
* Invoke request for given API operation. The request parameters, headers and payload are generated via specification
* details in that operation.
* @param path
* @param method
* @param operation
*/
private void sendRequest(String path, String method, OasOperation operation) {
if (operation.parameters != null) {
operation.parameters.stream()
.filter(param -> "header".equals(param.in))
.filter(param -> param.required)
.forEach(param -> clientSteps.addRequestHeader(param.getName(), OpenApiTestDataGenerator.createRandomValueExpression((OasSchema) param.schema, OasModelHelper.getSchemaDefinitions(openApiDoc), false)));
operation.parameters.stream()
.filter(param -> "query".equals(param.in))
.filter(param -> param.required)
.forEach(param -> clientSteps.addRequestQueryParam(param.getName(), OpenApiTestDataGenerator.createRandomValueExpression((OasSchema) param.schema)));
}
Optional<OasSchema> body = OasModelHelper.getRequestBodySchema(openApiDoc, operation);
if (body.isPresent()) {
clientSteps.setRequestBody(OpenApiTestDataGenerator.createOutboundPayload(body.get(), OasModelHelper.getSchemaDefinitions(openApiDoc)));
if (OasModelHelper.isReferenceType(body.get())
|| OasModelHelper.isObjectType(body.get())
|| OasModelHelper.isArrayType(body.get())) {
clientSteps.setOutboundDictionary(outboundDictionary);
}
}
String randomizedPath = path;
if (operation.parameters != null) {
List<OasParameter> pathParams = operation.parameters.stream()
.filter(p -> "path".equals(p.in))
.collect(Collectors.toList());
for (OasParameter parameter : pathParams) {
String parameterValue;
if (runner.getTestCase().getVariableDefinitions().containsKey(parameter.getName())) {
parameterValue = "\\" + CitrusSettings.VARIABLE_PREFIX + parameter.getName() + CitrusSettings.VARIABLE_SUFFIX;
} else {
parameterValue = OpenApiTestDataGenerator.createRandomValueExpression((OasSchema) parameter.schema);
}
randomizedPath = Pattern.compile("\\{" + parameter.getName() + "}")
.matcher(randomizedPath)
.replaceAll(parameterValue);
}
}
Optional<String> contentType = OasModelHelper.getRequestContentType(operation);
contentType.ifPresent(s -> clientSteps.addRequestHeader(HttpHeaders.CONTENT_TYPE, s));
clientSteps.sendClientRequest(method.toUpperCase(), randomizedPath);
}
示例11
/**
* @see io.apicurio.datamodels.combined.visitors.CombinedVisitorAdapter#visitOperation(io.apicurio.datamodels.core.models.common.Operation)
*/
@Override
public void visitOperation(Operation node) {
OasOperation op = (OasOperation) node;
CodegenJavaMethod method = new CodegenJavaMethod();
method.setName(this.methodName(op));
method.setPath(this.methodPath(op));
method.setMethod(node.getType());
method.setProduces(new HashSet<>());
method.setConsumes(new HashSet<>());
method.setArguments(new ArrayList<>());
if (node.description != null) { method.setDescription(node.description); }
// Handle 2.0 "produces" and "consumes"
if (node.ownerDocument().getDocumentType() == DocumentType.openapi2) {
List<String> produces = ((Oas20Operation) node).produces;
if (produces == null) {
produces = ((Oas20Document) node.ownerDocument()).produces;
}
if (produces != null) {
method.setProduces(new HashSet<>(produces));
}
List<String> consumes = ((Oas20Operation) node).consumes;
if (consumes == null) {
consumes = ((Oas20Document) node.ownerDocument()).consumes;
}
if (consumes != null) {
method.setConsumes(new HashSet<>(consumes));
}
}
boolean async = false;
Extension asyncExt = node.getExtension("x-codegen-async");
if (asyncExt != null && asyncExt.value != null) {
async = Boolean.valueOf(asyncExt.value.toString());
}
method.setAsync(async);
this._currentMethod = method;
this._currentInterface.getMethods().add(method);
// Be sure to process path and query parameters found on the parent!
this._processPathItemParams = true;
List<OasParameter> parentParams = ((OasPathItem) node.parent()).parameters;
if (parentParams != null && parentParams.size() > 0) {
for (OasParameter parentParam : parentParams) {
VisitorUtil.visitNode(parentParam, this);
}
}
this._processPathItemParams = false;
}
示例12
/**
* Determines if given parameter has a reference to another schema object.
* @param parameter to check
* @return true if given parameter has a reference.
*/
public static boolean isReferenceType(OasParameter parameter) {
return parameter.$ref != null;
}
示例13
/**
* Determines if given parameter lives in body.
* @param parameter to check.
* @return true if given parameter is a body parameter.
*/
public static boolean isBody(OasParameter parameter) {
return "body".equals(parameter.in);
}
示例14
/**
* Checks for serializable nature of given parameter. These are parameters that live in
* form data, query, header, request path or cookies.
* @param parameter to check.
* @return true if parameter is serializable.
*/
public static boolean isSerializable(OasParameter parameter) {
return Arrays.asList("formData", "query", "header", "path", "cookie").contains(parameter.in);
}