From 2e04ca0810ab39eea61f403b8a0801d02847c2a5 Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Fri, 14 Jul 2023 09:46:32 +0200 Subject: [PATCH 1/6] [KOGITO-9584] Ensure nested schemas are resolved for swagger doc --- .../workflow/actions/JsonSchemaValidator.java | 13 +++++++++---- .../workflow/parser/schema/JsonSchemaImpl.java | 7 +++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/JsonSchemaValidator.java b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/JsonSchemaValidator.java index 080580c2033..5d29e78ad68 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/JsonSchemaValidator.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/actions/JsonSchemaValidator.java @@ -28,6 +28,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.NullNode; +import com.networknt.schema.JsonSchema; import com.networknt.schema.JsonSchemaFactory; import com.networknt.schema.SpecVersion.VersionFlag; import com.networknt.schema.ValidationMessage; @@ -43,7 +44,7 @@ public class JsonSchemaValidator implements WorkflowModelValidator { protected final String schemaRef; protected final boolean failOnValidationErrors; - private final AtomicReference schemaObject = new AtomicReference<>(); + private final AtomicReference schemaObject = new AtomicReference<>(); public JsonSchemaValidator(String schema, boolean failOnValidationErrors) { this.schemaRef = schema; @@ -54,7 +55,7 @@ public JsonSchemaValidator(String schema, boolean failOnValidationErrors) { public void validate(Map model) { try { Set report = - JsonSchemaFactory.getInstance(VersionFlag.V4).getSchema(schemaData()).validate((JsonNode) model.getOrDefault(SWFConstants.DEFAULT_WORKFLOW_VAR, NullNode.instance)); + schema().validate((JsonNode) model.getOrDefault(SWFConstants.DEFAULT_WORKFLOW_VAR, NullNode.instance)); if (!report.isEmpty()) { StringBuilder sb = new StringBuilder("There are JsonSchema validation errors:"); report.forEach(m -> sb.append(System.lineSeparator()).append(m.getMessage())); @@ -70,9 +71,13 @@ public void validate(Map model) { } public JsonNode schemaData() throws IOException { - JsonNode result = schemaObject.get(); + return schema().getSchemaNode(); + } + + private JsonSchema schema() throws IOException { + JsonSchema result = schemaObject.get(); if (result == null) { - result = ObjectMapperFactory.get().readTree(readAllBytes(runtimeLoader(schemaRef))); + result = JsonSchemaFactory.getInstance(VersionFlag.V7).getSchema(ObjectMapperFactory.get().readTree(readAllBytes(runtimeLoader(schemaRef)))); schemaObject.set(result); } return result; diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java index 238b006b614..8cf2d1de5fb 100644 --- a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java @@ -23,6 +23,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import io.smallrye.openapi.api.models.media.SchemaImpl; @@ -35,6 +36,12 @@ @JsonInclude(JsonInclude.Include.NON_NULL) public class JsonSchemaImpl extends SchemaImpl { + @JsonSetter("$ref") + @Override + public void setRef(String ref) { + super.setRef(ref); + } + @JsonDeserialize(as = JsonSchemaImpl.class) @Override public Schema getItems() { From ddfd1d99180b78752cb57167984d6c3148f5126b Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Mon, 17 Jul 2023 11:51:56 +0200 Subject: [PATCH 2/6] [KOGITO-9584] Downloading external references --- .../parser/schema/JsonSchemaImpl.java | 26 +++++++++ .../schema/OpenApiModelSchemaGenerator.java | 3 + .../workflow/parser/schema/RefSchemas.java | 57 +++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/RefSchemas.java diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java index 8cf2d1de5fb..ee3deecc6ec 100644 --- a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java @@ -16,16 +16,24 @@ package org.kie.kogito.serverless.workflow.parser.schema; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; import java.util.List; import java.util.Map; +import java.util.Optional; import org.eclipse.microprofile.openapi.models.media.Schema; +import org.kie.kogito.jackson.utils.ObjectMapperFactory; +import org.kie.kogito.serverless.workflow.io.URIContentLoaderFactory; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import io.smallrye.openapi.api.constants.OpenApiConstants; import io.smallrye.openapi.api.models.media.SchemaImpl; /** @@ -39,6 +47,24 @@ public class JsonSchemaImpl extends SchemaImpl { @JsonSetter("$ref") @Override public void setRef(String ref) { + if (ref != null && !ref.startsWith("#")) { + try (InputStream is = URIContentLoaderFactory.loader(new URI(ref), Optional.empty(), Optional.empty(), Optional.empty(), null).getInputStream()) { + JsonSchemaImpl schema = ObjectMapperFactory.get().readValue(is.readAllBytes(), JsonSchemaImpl.class); + String key; + if (schema.getTitle() == null) { + key = RefSchemas.getKey(); + schema.title(key); + } else { + key = schema.getTitle(); + } + if (key != null) { + RefSchemas.get().put(key, schema); + } + ref = OpenApiConstants.REF_PREFIX_SCHEMA + key; + } catch (URISyntaxException | IOException e) { + // if not a valid uri, let super handle it + } + } super.setRef(ref); } diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/OpenApiModelSchemaGenerator.java b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/OpenApiModelSchemaGenerator.java index 4759f8b5436..4899c66b7fc 100644 --- a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/OpenApiModelSchemaGenerator.java +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/OpenApiModelSchemaGenerator.java @@ -58,6 +58,7 @@ private OpenApiModelSchemaGenerator() { public static void addOpenAPIModelSchema(KogitoWorkflowProcess workflow, Map schemas) { if (workflow instanceof WorkflowProcess) { + RefSchemas.init(workflow.getId()); WorkflowProcess workflowProcess = (WorkflowProcess) workflow; getSchema(workflowProcess.getInputValidator()).ifPresent(v -> { String key = getSchemaName(workflow.getId(), INPUT_SUFFIX); @@ -67,6 +68,8 @@ public static void addOpenAPIModelSchema(KogitoWorkflowProcess workflow, Map map = new HashMap<>(); + + public ThreadInfo(String id) { + this.id = id; + } + } + + private static ThreadLocal threadInfo = new ThreadLocal<>(); + + public static void init(String id) { + threadInfo.set(new ThreadInfo(id)); + } + + public static Map get() { + return threadInfo.get().map; + } + + public static String getKey() { + ThreadInfo t = threadInfo.get(); + return t.id + "_nested_" + ++t.counter; + } + + public static void reset() { + threadInfo.set(null); + } +} From abb367434f7a9dcb5489c7c8a363d9ecdcfbc5eb Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Mon, 17 Jul 2023 17:31:53 +0200 Subject: [PATCH 3/6] [KOGITO-9584] Review comments and sonar warnings --- .../workflow/parser/schema/JsonSchemaImpl.java | 5 +++++ .../serverless/workflow/parser/schema/RefSchemas.java | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java index ee3deecc6ec..abda815211d 100644 --- a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java @@ -27,6 +27,8 @@ import org.eclipse.microprofile.openapi.models.media.Schema; import org.kie.kogito.jackson.utils.ObjectMapperFactory; import org.kie.kogito.serverless.workflow.io.URIContentLoaderFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; @@ -44,6 +46,8 @@ @JsonInclude(JsonInclude.Include.NON_NULL) public class JsonSchemaImpl extends SchemaImpl { + private static final Logger logger = LoggerFactory.getLogger(JsonSchemaImpl.class); + @JsonSetter("$ref") @Override public void setRef(String ref) { @@ -63,6 +67,7 @@ public void setRef(String ref) { ref = OpenApiConstants.REF_PREFIX_SCHEMA + key; } catch (URISyntaxException | IOException e) { // if not a valid uri, let super handle it + logger.info("Error loading ref {}", ref, e); } } super.setRef(ref); diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/RefSchemas.java b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/RefSchemas.java index 3c3571ab0ce..3020acf1562 100644 --- a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/RefSchemas.java +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/RefSchemas.java @@ -27,11 +27,11 @@ private RefSchemas() { } private static class ThreadInfo { - public int counter; - public String id; - public Map map = new HashMap<>(); + private final String id; + private final Map map = new HashMap<>(); + private int counter; - public ThreadInfo(String id) { + private ThreadInfo(String id) { this.id = id; } } @@ -52,6 +52,6 @@ public static String getKey() { } public static void reset() { - threadInfo.set(null); + threadInfo.remove(); } } From bfcfefcc17fed7f94b1feadee4d90938be3cee4c Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Tue, 18 Jul 2023 18:34:05 +0200 Subject: [PATCH 4/6] [KOGITO-9584] Support id --- .../utils/ServerlessWorkflowUtils.java | 8 +- .../workflow/io/URIContentLoaderFactory.java | 92 +++++++++++++++++-- .../workflow/io/URIContentLoaderType.java | 3 - .../parser/schema/JsonSchemaImpl.java | 9 +- .../schema/OpenApiModelSchemaGenerator.java | 25 ++--- .../workflow/parser/schema/RefSchemas.java | 9 ++ 6 files changed, 116 insertions(+), 30 deletions(-) diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/utils/ServerlessWorkflowUtils.java b/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/utils/ServerlessWorkflowUtils.java index 4795b1e00ef..7c9de936772 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/utils/ServerlessWorkflowUtils.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/utils/ServerlessWorkflowUtils.java @@ -36,6 +36,7 @@ import org.kie.kogito.serverless.workflow.extensions.OutputSchema; import org.kie.kogito.serverless.workflow.extensions.URIDefinitions; import org.kie.kogito.serverless.workflow.io.URIContentLoaderFactory; +import org.kie.kogito.serverless.workflow.io.URIContentLoaderFactory.Builder; import org.kie.kogito.serverless.workflow.models.JsonNodeModel; import org.kie.kogito.serverless.workflow.parser.ParserContext; import org.kie.kogito.serverless.workflow.suppliers.ConfigWorkItemSupplier; @@ -195,9 +196,10 @@ public static Optional loadResourceFile(Workflow workflow, Optional loadResourceFile(String uriStr, Optional workflow, Optional parserContext, String authRef) { final URI uri = URI.create(uriStr); try { - final byte[] bytes = - URIContentLoaderFactory.readAllBytes(URIContentLoaderFactory.loader(uri, parserContext.map(p -> p.getContext().getClassLoader()), Optional.empty(), workflow, authRef)); - return Optional.of(bytes); + Builder builder = URIContentLoaderFactory.builder(uri).withAuthRef(authRef); + workflow.ifPresent(builder::withWorkflow); + parserContext.map(p -> p.getContext().getClassLoader()).ifPresent(builder::withClassloader); + return Optional.of(URIContentLoaderFactory.readAllBytes(builder.build())); } catch (UncheckedIOException io) { // if file cannot be found in build context, warn it and return the unmodified uri (it might be possible that later the resource is available at runtime) logger.warn("Resource {} cannot be found at build time, ignoring", uri, io); diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java index ce374d33d2f..7013f0a93cd 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java @@ -35,23 +35,95 @@ public static byte[] readAllBytes(URIContentLoader loader) { public static URIContentLoader runtimeLoader(String uriStr) { URI uri = URI.create(uriStr); - return loader(uri, Optional.empty(), Optional.of(new ClassPathContentLoader(uri, Optional.empty())), Optional.empty(), null); + Builder builder = new Builder(uri); + builder.withFallback(new ClassPathContentLoader(uri, Optional.empty())); + return builder.build(); + } public static URIContentLoader buildLoader(URI uri, ClassLoader cl, Workflow workflow, String authRef) { - return loader(uri, Optional.of(cl), Optional.empty(), Optional.of(workflow), authRef); + return new Builder(uri).withClassloader(cl).withWorkflow(workflow).withAuthRef(authRef).build(); } + /** + * @deprecated Use builder + */ + @Deprecated public static URIContentLoader loader(URI uri, Optional cl, Optional fallback, Optional workflow, String authRef) { - switch (URIContentLoaderType.from(uri)) { - case FILE: - return new FileContentLoader(uri, fallback); - case HTTP: - return new HttpContentLoader(uri, fallback, workflow, authRef); - default: - case CLASSPATH: - return new ClassPathContentLoader(uri, cl); + Builder builder = new Builder(uri); + cl.ifPresent(builder::withClassloader); + fallback.ifPresent(builder::withFallback); + workflow.ifPresent(builder::withWorkflow); + builder.withAuthRef(authRef); + return builder.build(); + } + + public static Builder builder(URI uri) { + return new Builder(uri); + } + + public static class Builder { + private URI uri; + private ClassLoader cl; + private URIContentLoader fallback; + private Workflow workflow; + private String authRef; + private URI baseURI; + + private Builder(URI uri) { + this.uri = uri; + } + + public Builder withClassloader(ClassLoader cl) { + this.cl = cl; + return this; + } + + public Builder withFallback(URIContentLoader fallback) { + this.fallback = fallback; + return this; } + + public Builder withWorkflow(Workflow workflow) { + this.workflow = workflow; + return this; + } + + public Builder withAuthRef(String authRef) { + this.authRef = authRef; + return this; + } + + public Builder withBaseURI(String baseURI) { + if (baseURI != null) { + int lastIndexOf = baseURI.lastIndexOf('/'); + this.baseURI = URI.create(lastIndexOf != -1 ? baseURI.substring(0, lastIndexOf) : baseURI); + } + return this; + } + + public URIContentLoader build() { + if (uri.getScheme() == null) { + if (baseURI != null) { + uri = compoundURI(baseURI, uri); + } else { + return new ClassPathContentLoader(uri, Optional.ofNullable(cl)); + } + } + switch (URIContentLoaderType.from(uri)) { + case FILE: + return new FileContentLoader(uri, Optional.ofNullable(fallback)); + case HTTP: + return new HttpContentLoader(uri, Optional.ofNullable(fallback), Optional.ofNullable(workflow), authRef); + default: + case CLASSPATH: + return new ClassPathContentLoader(uri, Optional.ofNullable(cl)); + } + } + } + + private static URI compoundURI(URI baseURI, URI uri) { + return URI.create(baseURI.toString() + "/" + uri.toString()); } private URIContentLoaderFactory() { diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderType.java b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderType.java index 3920305bf46..649294ad9c7 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderType.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderType.java @@ -23,9 +23,6 @@ public enum URIContentLoaderType { HTTP; public static URIContentLoaderType from(URI uri) { - if (uri.getScheme() == null) { - return CLASSPATH; - } switch (uri.getScheme().toLowerCase()) { case "file": return FILE; diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java index abda815211d..60fd6221afc 100644 --- a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/JsonSchemaImpl.java @@ -22,7 +22,6 @@ import java.net.URISyntaxException; import java.util.List; import java.util.Map; -import java.util.Optional; import org.eclipse.microprofile.openapi.models.media.Schema; import org.kie.kogito.jackson.utils.ObjectMapperFactory; @@ -48,11 +47,16 @@ public class JsonSchemaImpl extends SchemaImpl { private static final Logger logger = LoggerFactory.getLogger(JsonSchemaImpl.class); + @JsonSetter("$id") + public void setId(String id) { + RefSchemas.baseURI(id); + } + @JsonSetter("$ref") @Override public void setRef(String ref) { if (ref != null && !ref.startsWith("#")) { - try (InputStream is = URIContentLoaderFactory.loader(new URI(ref), Optional.empty(), Optional.empty(), Optional.empty(), null).getInputStream()) { + try (InputStream is = URIContentLoaderFactory.builder(new URI(ref)).withBaseURI(RefSchemas.getBaseURI()).build().getInputStream()) { JsonSchemaImpl schema = ObjectMapperFactory.get().readValue(is.readAllBytes(), JsonSchemaImpl.class); String key; if (schema.getTitle() == null) { @@ -114,5 +118,4 @@ public Schema getAdditionalPropertiesSchema() { public Schema getNot() { return super.getNot(); } - } diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/OpenApiModelSchemaGenerator.java b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/OpenApiModelSchemaGenerator.java index 4899c66b7fc..68756ba8e58 100644 --- a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/OpenApiModelSchemaGenerator.java +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/OpenApiModelSchemaGenerator.java @@ -58,18 +58,21 @@ private OpenApiModelSchemaGenerator() { public static void addOpenAPIModelSchema(KogitoWorkflowProcess workflow, Map schemas) { if (workflow instanceof WorkflowProcess) { - RefSchemas.init(workflow.getId()); WorkflowProcess workflowProcess = (WorkflowProcess) workflow; - getSchema(workflowProcess.getInputValidator()).ifPresent(v -> { - String key = getSchemaName(workflow.getId(), INPUT_SUFFIX); - schemas.put(key, schemaTitle(key, v)); - }); - getSchema(workflowProcess.getOutputValidator()).ifPresent(v -> { - String key = getSchemaName(workflow.getId(), OUTPUT_SUFFIX); - schemas.put(key, createOutputSchema(schemaTitle(key, v))); - }); - schemas.putAll(RefSchemas.get()); - RefSchemas.reset(); + RefSchemas.init(workflow.getId()); + try { + getSchema(workflowProcess.getInputValidator()).ifPresent(v -> { + String key = getSchemaName(workflow.getId(), INPUT_SUFFIX); + schemas.put(key, schemaTitle(key, v)); + }); + getSchema(workflowProcess.getOutputValidator()).ifPresent(v -> { + String key = getSchemaName(workflow.getId(), OUTPUT_SUFFIX); + schemas.put(key, createOutputSchema(schemaTitle(key, v))); + }); + schemas.putAll(RefSchemas.get()); + } finally { + RefSchemas.reset(); + } } } diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/RefSchemas.java b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/RefSchemas.java index 3020acf1562..04291d6ce43 100644 --- a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/RefSchemas.java +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/RefSchemas.java @@ -30,6 +30,7 @@ private static class ThreadInfo { private final String id; private final Map map = new HashMap<>(); private int counter; + private String baseURI; private ThreadInfo(String id) { this.id = id; @@ -46,6 +47,14 @@ public static Map get() { return threadInfo.get().map; } + public static void baseURI(String baseURI) { + threadInfo.get().baseURI = baseURI; + } + + public static String getBaseURI() { + return threadInfo.get().baseURI; + } + public static String getKey() { ThreadInfo t = threadInfo.get(); return t.id + "_nested_" + ++t.counter; From 28b3577b0fe0839be85975c96b7dd03f7eb38e40 Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Wed, 19 Jul 2023 13:47:03 +0200 Subject: [PATCH 5/6] [KOGITO-9584] Michaels comment --- .../workflow/io/URIContentLoaderFactory.java | 11 ++++------- .../serverless/workflow/parser/schema/RefSchemas.java | 5 ++++- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java index 7013f0a93cd..57662561be9 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java @@ -68,7 +68,7 @@ public static class Builder { private URIContentLoader fallback; private Workflow workflow; private String authRef; - private URI baseURI; + private String baseURI; private Builder(URI uri) { this.uri = uri; @@ -95,10 +95,7 @@ public Builder withAuthRef(String authRef) { } public Builder withBaseURI(String baseURI) { - if (baseURI != null) { - int lastIndexOf = baseURI.lastIndexOf('/'); - this.baseURI = URI.create(lastIndexOf != -1 ? baseURI.substring(0, lastIndexOf) : baseURI); - } + this.baseURI = baseURI; return this; } @@ -122,8 +119,8 @@ public URIContentLoader build() { } } - private static URI compoundURI(URI baseURI, URI uri) { - return URI.create(baseURI.toString() + "/" + uri.toString()); + private static URI compoundURI(String baseURI, URI uri) { + return URI.create(baseURI + "/" + uri.toString()); } private URIContentLoaderFactory() { diff --git a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/RefSchemas.java b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/RefSchemas.java index 04291d6ce43..f68072a5978 100644 --- a/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/RefSchemas.java +++ b/quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/serverless/workflow/parser/schema/RefSchemas.java @@ -48,7 +48,10 @@ public static Map get() { } public static void baseURI(String baseURI) { - threadInfo.get().baseURI = baseURI; + if (baseURI != null) { + int lastIndexOf = baseURI.lastIndexOf('/'); + threadInfo.get().baseURI = lastIndexOf != -1 ? baseURI.substring(0, lastIndexOf) : baseURI; + } } public static String getBaseURI() { From 5e65004ef8f9dc461407bf0b8792a1edad7eceab Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Thu, 20 Jul 2023 14:28:12 +0200 Subject: [PATCH 6/6] [KOGITO-9584] Formatting issue --- .../serverless/workflow/utils/ServerlessWorkflowUtils.java | 6 +++--- .../serverless/workflow/io/URIContentLoaderFactory.java | 6 +----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/utils/ServerlessWorkflowUtils.java b/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/utils/ServerlessWorkflowUtils.java index 7c9de936772..03ae00308f4 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/utils/ServerlessWorkflowUtils.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-builder/src/main/java/org/kie/kogito/serverless/workflow/utils/ServerlessWorkflowUtils.java @@ -196,9 +196,9 @@ public static Optional loadResourceFile(Workflow workflow, Optional loadResourceFile(String uriStr, Optional workflow, Optional parserContext, String authRef) { final URI uri = URI.create(uriStr); try { - Builder builder = URIContentLoaderFactory.builder(uri).withAuthRef(authRef); - workflow.ifPresent(builder::withWorkflow); - parserContext.map(p -> p.getContext().getClassLoader()).ifPresent(builder::withClassloader); + Builder builder = URIContentLoaderFactory.builder(uri).withAuthRef(authRef); + workflow.ifPresent(builder::withWorkflow); + parserContext.map(p -> p.getContext().getClassLoader()).ifPresent(builder::withClassloader); return Optional.of(URIContentLoaderFactory.readAllBytes(builder.build())); } catch (UncheckedIOException io) { // if file cannot be found in build context, warn it and return the unmodified uri (it might be possible that later the resource is available at runtime) diff --git a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java index 57662561be9..1ea401d4874 100644 --- a/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java +++ b/kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/io/URIContentLoaderFactory.java @@ -102,7 +102,7 @@ public Builder withBaseURI(String baseURI) { public URIContentLoader build() { if (uri.getScheme() == null) { if (baseURI != null) { - uri = compoundURI(baseURI, uri); + uri = URI.create(baseURI + "/" + uri.toString()); } else { return new ClassPathContentLoader(uri, Optional.ofNullable(cl)); } @@ -119,10 +119,6 @@ public URIContentLoader build() { } } - private static URI compoundURI(String baseURI, URI uri) { - return URI.create(baseURI + "/" + uri.toString()); - } - private URIContentLoaderFactory() { } }