in components/camel-openapi-java/src/main/java/org/apache/camel/openapi/RestOpenApiReader.java [524:712]
private void doParseVerb(
CamelContext camelContext, OpenAPI openApi, VerbDefinition verb, Operation op, String consumes,
String produces) {
if (verb.getDescriptionText() != null) {
op.setSummary(getValue(camelContext, verb.getDescriptionText()));
}
if ("true".equals(verb.getDeprecated())) {
op.setDeprecated(Boolean.TRUE);
}
// security
for (SecurityDefinition sd : verb.getSecurity()) {
List<String> scopes = new ArrayList<>();
if (sd.getScopes() != null) {
for (String scope : ObjectHelper.createIterable(getValue(camelContext, sd.getScopes()))) {
scopes.add(scope);
}
}
SecurityRequirement securityRequirement = new SecurityRequirement(); //op.createSecurityRequirement();
securityRequirement.addList(getValue(camelContext, sd.getKey()), scopes);
op.addSecurityItem(securityRequirement);
}
for (ParamDefinition param : verb.getParams()) {
Parameter parameter = new Parameter().in(param.getType().name());
if (parameter != null) {
parameter.setName(getValue(camelContext, param.getName()));
if (org.apache.camel.util.ObjectHelper.isNotEmpty(param.getDescription())) {
parameter.setDescription(getValue(camelContext, param.getDescription()));
}
parameter.setRequired(param.getRequired());
final String dataType
= getValue(camelContext, param.getDataType() != null ? param.getDataType() : "string");
// set type on parameter
if (!"body".equals(parameter.getIn())) {
Schema schema = new Schema<>();
final boolean isArray = "array".equalsIgnoreCase(dataType);
final List<String> allowableValues = getValue(camelContext, param.getAllowableValuesAsStringList());
final boolean hasAllowableValues = allowableValues != null && !allowableValues.isEmpty();
parameter.setSchema(schema);
schema.setType(dataType);
if (openApi.getSpecVersion().equals(SpecVersion.V31)) {
schema.addType(dataType);
}
if (param.getDataFormat() != null) {
schema.setFormat(getValue(camelContext, param.getDataFormat()));
}
if (isArray) {
String arrayType = getValue(camelContext, param.getArrayType());
if (arrayType != null) {
if (arrayType.equalsIgnoreCase("string")) {
defineSchemas(parameter, allowableValues, String.class);
}
if (arrayType.equalsIgnoreCase("int") || arrayType.equalsIgnoreCase("integer")) {
defineSchemas(parameter, allowableValues, Integer.class);
}
if (arrayType.equalsIgnoreCase("long")) {
defineSchemas(parameter, allowableValues, Long.class);
}
if (arrayType.equalsIgnoreCase("float")) {
defineSchemas(parameter, allowableValues, Float.class);
}
if (arrayType.equalsIgnoreCase("double")) {
defineSchemas(parameter, allowableValues, Double.class);
}
if (arrayType.equalsIgnoreCase("boolean")) {
defineSchemas(parameter, allowableValues, Boolean.class);
}
if (arrayType.equalsIgnoreCase("byte")) {
defineSchemas(parameter, allowableValues, ByteArraySchema.class);
}
if (arrayType.equalsIgnoreCase("binary")) {
defineSchemas(parameter, allowableValues, BinarySchema.class);
}
if (arrayType.equalsIgnoreCase("date")) {
defineSchemas(parameter, allowableValues, DateSchema.class);
}
if (arrayType.equalsIgnoreCase("date-time")) {
defineSchemas(parameter, allowableValues, DateTimeSchema.class);
}
if (arrayType.equalsIgnoreCase("password")) {
defineSchemas(parameter, allowableValues, PasswordSchema.class);
}
}
}
if (param.getCollectionFormat() != null) {
parameter.setStyle(convertToOpenApiStyle(getValue(camelContext, param.getCollectionFormat().name())));
}
if (hasAllowableValues && !isArray) {
schema.setEnum(allowableValues);
}
// set default value on parameter
if (org.apache.camel.util.ObjectHelper.isNotEmpty(param.getDefaultValue())) {
schema.setDefault(getValue(camelContext, param.getDefaultValue()));
}
// add examples
if (param.getExamples() != null && !param.getExamples().isEmpty()) {
// Examples can be added with a key or a single one with no key
for (RestPropertyDefinition example : param.getExamples()) {
if (example.getKey().isEmpty()) {
if (parameter.getExample() != null) {
LOG.warn("The parameter already has an example with no key!");
}
parameter.setExample(example.getValue());
} else {
parameter.addExample(example.getKey(), new Example().value(example.getValue()));
}
}
}
op.addParametersItem(parameter);
}
// In OpenAPI 3x, body or form parameters are replaced by requestBody
if (parameter.getIn().equals("body")) {
RequestBody reqBody = new RequestBody().content(new Content());
reqBody.setRequired(param.getRequired());
reqBody.setDescription(getValue(camelContext, param.getDescription()));
op.setRequestBody(reqBody);
String type = getValue(camelContext, verb.getType());
if (type == null) {
type = dataType;
}
Schema<?> bodySchema = null;
if (type != null) {
if (type.endsWith("[]")) {
bodySchema = modelTypeAsProperty(type, openApi);
} else {
String ref = modelTypeAsRef(type, openApi);
if (ref != null) {
bodySchema = new Schema().$ref(OAS30_SCHEMA_DEFINITION_PREFIX + ref);
} else {
bodySchema = modelTypeAsProperty(type, openApi);
}
}
}
if (consumes != null) {
String[] parts = consumes.split(",");
for (String part : parts) {
MediaType mediaType = new MediaType().schema(bodySchema);
if (param.getExamples() != null) {
for (RestPropertyDefinition example : param.getExamples()) {
if (part.equals(example.getKey())) {
mediaType.setExample(example.getValue());
}
// TODO: Check for non-matched or empty key
}
}
reqBody.getContent().addMediaType(part, mediaType);
}
}
}
}
}
// clear parameters if its empty
if (op.getParameters() != null && op.getParameters().isEmpty()) {
// op.parameters.clear();
op.setParameters(null); // Is this necessary?
}
// if we have an out type then set that as response message
if (verb.getOutType() != null) {
if (op.getResponses() == null) {
op.setResponses(new ApiResponses());
}
String[] parts;
if (produces != null) {
parts = produces.split(",");
for (String produce : parts) {
ApiResponse response = new ApiResponse().description("Output type"); // ??
Content responseContent = new Content();
MediaType contentType = new MediaType();
responseContent.addMediaType(produce, contentType);
Schema<?> model = modelTypeAsProperty(getValue(camelContext, verb.getOutType()), openApi);
contentType.setSchema(model);
response.setContent(responseContent);
op.getResponses().addApiResponse("200", response);
}
}
}
}