Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions impl/openapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,15 @@
<artifactId>swagger-parser</artifactId>
<version>${version.io.swagger.parser.v3}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import io.serverlessworkflow.impl.executors.http.HttpExecutor.HttpExecutorBuilder;
import io.serverlessworkflow.impl.resources.ResourceLoaderUtils;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -110,7 +109,7 @@ private void fillHttpBuilder(WorkflowApplication application, OperationDefinitio
Set<String> missingParams = new HashSet<>();

Map<String, Object> bodyParameters = new HashMap<>(parameters);
for (Parameter parameter : operation.getParameters()) {
for (ParameterDefinition parameter : operation.getParameters()) {
switch (parameter.getIn()) {
case "header":
param(parameter, bodyParameters, headersMap, missingParams);
Expand Down Expand Up @@ -141,7 +140,7 @@ private void fillHttpBuilder(WorkflowApplication application, OperationDefinitio
}

private void param(
Parameter parameter,
ParameterDefinition parameter,
Map<String, Object> origMap,
Map<String, Object> collectorMap,
Set<String> missingParams) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/
package io.serverlessworkflow.impl.executors.openapi;

import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import java.util.Set;

class OpenAPIProcessor {
Expand All @@ -31,14 +32,22 @@ class OpenAPIProcessor {
}

public OperationDefinition parse(String content) {
OpenAPIV3Parser parser = new OpenAPIV3Parser();
OpenAPIParser parser = new OpenAPIParser();
ParseOptions opts = new ParseOptions();
opts.setResolve(true);
opts.setResolveFully(false);
return getOperation(parser.readContents(content).getOpenAPI());
opts.setResolveFully(true);

SwaggerParseResult result = parser.readContents(content, null, opts);

if (result.getMessages() != null && !result.getMessages().isEmpty()) {
throw new IllegalArgumentException(
"Failed to parse OpenAPI document: " + String.join(", ", result.getMessages()));
}
return getOperation(result.getOpenAPI(), !result.isOpenapi31());
}

private OperationDefinition getOperation(OpenAPI openAPI) {
private OperationDefinition getOperation(
OpenAPI openAPI, boolean emulateSwaggerV2BodyParameters) {
if (openAPI == null || openAPI.getPaths() == null) {
throw new IllegalArgumentException("Invalid OpenAPI document");
}
Expand All @@ -50,7 +59,11 @@ private OperationDefinition getOperation(OpenAPI openAPI) {
OperationAndMethod operationAndMethod = findInPathItem(pathItem, operationId);
if (operationAndMethod != null) {
return new OperationDefinition(
openAPI, operationAndMethod.operation, path, operationAndMethod.method);
openAPI,
operationAndMethod.operation,
path,
operationAndMethod.method,
emulateSwaggerV2BodyParameters);
}
}
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,33 @@

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.servers.Server;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

class OperationDefinition {
private final Operation operation;
private final String method;
private final OpenAPI openAPI;
private final String path;
private final boolean emulateSwaggerV2BodyParameters;

OperationDefinition(OpenAPI openAPI, Operation operation, String path, String method) {
OperationDefinition(
OpenAPI openAPI,
Operation operation,
String path,
String method,
boolean emulateSwaggerV2BodyParameters) {
this.openAPI = openAPI;
this.operation = operation;
this.path = path;
this.method = method;
this.emulateSwaggerV2BodyParameters = emulateSwaggerV2BodyParameters;
}

String getMethod() {
Expand All @@ -52,67 +59,66 @@ Operation getOperation() {
}

List<String> getServers() {
if (openAPI.getServers() == null) {
return List.of();
}
return openAPI.getServers().stream().map(Server::getUrl).toList();
}

List<Parameter> getParameters() {
List<ParameterDefinition> getParameters() {
return emulateSwaggerV2BodyParameters ? getSwaggerV2Parameters() : getOpenApiParameters();
}

private List<ParameterDefinition> getOpenApiParameters() {
if (operation.getParameters() == null) {
return List.of();
}
return operation.getParameters();
return operation.getParameters().stream().map(ParameterDefinition::new).toList();
}

@SuppressWarnings({"rawtypes", "unchecked"})
Map<String, Schema> getBody() {
if (operation.getRequestBody() != null && operation.getRequestBody().getContent() != null) {
Content content = operation.getRequestBody().getContent();
if (content.containsKey("application/json")) {
MediaType mt = content.get("application/json");
if (mt.getSchema().get$ref() != null && !mt.getSchema().get$ref().isEmpty()) {
Schema<?> schema = resolveSchema(mt.getSchema().get$ref());
return schema.getProperties();
} else if (mt.getSchema().getProperties() != null) {
return mt.getSchema().getProperties();
} else {
throw new IllegalArgumentException(
"Can't resolve schema for request body of operation " + operation.getOperationId());
}
} else {
throw new IllegalArgumentException("Only 'application/json' content type is supported");
}
@SuppressWarnings({"rawtypes"})
private List<ParameterDefinition> getSwaggerV2Parameters() {
if (operation.getParameters() != null && !operation.getParameters().isEmpty()) {
return operation.getParameters().stream().map(ParameterDefinition::new).toList();
}
return Map.of();
}

String getContentType() {
String method = getMethod().toUpperCase();

if (method.equals("POST") || method.equals("PUT") || method.equals("PATCH")) {
if (operation.getRequestBody() != null && operation.getRequestBody().getContent() != null) {
Content content = operation.getRequestBody().getContent();
if (!content.isEmpty()) {
return content.keySet().iterator().next();
}
if (operation.getRequestBody() != null) {
Schema<?> schema = null;
if (operation.getRequestBody().getContent() != null
&& operation
.getRequestBody()
.getContent()
.containsKey(jakarta.ws.rs.core.MediaType.APPLICATION_JSON)) {
MediaType mt =
operation
.getRequestBody()
.getContent()
.get(jakarta.ws.rs.core.MediaType.APPLICATION_JSON);
schema = mt.getSchema();
} else if (operation.getRequestBody().get$ref() != null) {
schema = resolveSchema(operation.getRequestBody().get$ref());
}
}

if (operation.getResponses() != null) {
for (String code : new String[] {"200", "201", "204"}) {
ApiResponse resp = operation.getResponses().get(code);
if (resp != null && resp.getContent() != null && !resp.getContent().isEmpty()) {
return resp.getContent().keySet().iterator().next();
}
if (schema == null) {
return List.of();
}
for (Map.Entry<String, ApiResponse> e : operation.getResponses().entrySet()) {
Content content = e.getValue().getContent();
if (content != null && !content.isEmpty()) {
return content.keySet().iterator().next();

Set<String> required =
schema.getRequired() != null ? new HashSet<>(schema.getRequired()) : new HashSet<>();

Map<String, Schema> properties = schema.getProperties();
if (properties != null) {
List<ParameterDefinition> result = new ArrayList<>();
for (Map.Entry<String, Schema> prop : properties.entrySet()) {
String fieldName = prop.getKey();
ParameterDefinition fieldParam =
new ParameterDefinition(
fieldName, "body", required.contains(fieldName), prop.getValue());
result.add(fieldParam);
}
return result;
}
}

throw new IllegalStateException(
"No content type found for operation " + operation.getOperationId() + " [" + method + "]");
return List.of();
}

Schema<?> resolveSchema(String ref) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2020-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.serverlessworkflow.impl.executors.openapi;

import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.parameters.Parameter;

class ParameterDefinition {

private final String name;
private final String in;
private final boolean required;
private final Schema schema;

ParameterDefinition(Parameter parameter) {
this(
parameter.getName(),
parameter.getIn(),
parameter.getRequired() != null && parameter.getRequired(),
parameter.getSchema());
}

ParameterDefinition(String name, String in, boolean required, Schema schema) {
this.name = name;
this.in = in;
this.required = required;
this.schema = schema;
}

public String getIn() {
return in;
}

public String getName() {
return name;
}

public boolean getRequired() {
return required;
}

public Schema getSchema() {
return schema;
}
}
Loading