-
Notifications
You must be signed in to change notification settings - Fork 53
Add automatic URI detection for OpenAPI document configuration #1319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,4 +29,7 @@ target/ | |
| build/ | ||
|
|
||
| ### VS Code ### | ||
| .vscode/ | ||
| .vscode/ | ||
|
|
||
| # Bob-Shell | ||
| .bob/notes/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /* | ||
| * 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.fluent.test; | ||
|
|
||
| import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.openapi; | ||
|
|
||
| import io.serverlessworkflow.fluent.func.FuncWorkflowBuilder; | ||
| import io.serverlessworkflow.impl.WorkflowApplication; | ||
| import io.serverlessworkflow.impl.WorkflowDefinition; | ||
| import io.serverlessworkflow.impl.WorkflowInstance; | ||
| import io.serverlessworkflow.impl.WorkflowModel; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.util.Map; | ||
| import mockwebserver3.MockResponse; | ||
| import mockwebserver3.MockWebServer; | ||
| import okhttp3.Headers; | ||
| import org.assertj.core.api.SoftAssertions; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class FuncOpenAPITest { | ||
|
|
||
| private static MockWebServer mockWebServer; | ||
|
|
||
| @BeforeEach | ||
| public void setup() throws IOException { | ||
| mockWebServer = new MockWebServer(); | ||
| mockWebServer.start(0); | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void tearDown() { | ||
| mockWebServer.close(); | ||
| } | ||
|
|
||
| @Test | ||
| void test_openapi_document_with_non_jq_uri_string() { | ||
| String mockedSwaggerDoc = | ||
| """ | ||
| { | ||
| "swagger": "2.0", | ||
| "info": { "version": "1.0.0", "title": "Mock Petstore" }, | ||
| "host": "localhost:%d", | ||
| "basePath": "/v2", | ||
| "schemes": [ "http" ], | ||
| "paths": { | ||
| "/pet/findByStatus": { | ||
| "get": { | ||
| "operationId": "findPetsByStatus", | ||
| "parameters": [ | ||
| { | ||
| "name": "status", | ||
| "in": "query", | ||
| "required": true, | ||
| "type": "string" | ||
| } | ||
| ], | ||
| "responses": { "200": { "description": "OK" } } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """ | ||
| .formatted(mockWebServer.getPort()); | ||
|
|
||
| mockWebServer.enqueue( | ||
| new MockResponse(200, Headers.of("Content-Type", "application/json"), mockedSwaggerDoc)); | ||
| mockWebServer.enqueue( | ||
| new MockResponse( | ||
| 200, | ||
| Headers.of("Content-Type", "application/json"), | ||
| """ | ||
| { "description": "OK" } | ||
| """)); | ||
| var w = | ||
| FuncWorkflowBuilder.workflow("openapi-call-workflow") | ||
| .tasks( | ||
| openapi() | ||
| .document(URI.create(mockWebServer.url("/v2/swagger.json").toString())) | ||
| .operation("findPetsByStatus") | ||
| .parameters(Map.of("status", "available"))) | ||
|
Comment on lines
+91
to
+96
|
||
| .build(); | ||
|
Comment on lines
+51
to
+97
|
||
|
|
||
| try (WorkflowApplication app = WorkflowApplication.builder().build()) { | ||
|
|
||
| WorkflowDefinition def = app.workflowDefinition(w); | ||
| WorkflowInstance instance = def.instance(Map.of()); | ||
| WorkflowModel model = instance.start().join(); | ||
|
|
||
| SoftAssertions.assertSoftly( | ||
| softly -> { | ||
| softly.assertThat(model).isNotNull(); | ||
| softly.assertThat(model.asMap()).contains(Map.of("description", "OK")); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| import io.serverlessworkflow.api.types.EndpointUri; | ||
| import io.serverlessworkflow.api.types.ExternalResource; | ||
| import io.serverlessworkflow.api.types.OpenAPIArguments; | ||
| import io.serverlessworkflow.api.types.ReferenceableAuthenticationPolicy; | ||
| import io.serverlessworkflow.api.types.UriTemplate; | ||
| import io.serverlessworkflow.fluent.spec.ReferenceableAuthenticationPolicyBuilder; | ||
| import io.serverlessworkflow.fluent.spec.TaskBaseBuilder; | ||
|
|
@@ -40,11 +41,19 @@ default CallOpenAPI build() { | |
|
|
||
| SELF self(); | ||
|
|
||
| /** | ||
| * Sets the OpenAPI document location. This method automatically detects whether the provided | ||
| * string is a literal URI or a JQ runtime expression. | ||
| * | ||
| * @param uri the OpenAPI document location as either a literal URI string or a JQ expression | ||
| * @return this builder instance for method chaining | ||
| * @see #document(URI) for setting a literal URI directly | ||
| * @see #document(String, AuthenticationConfigurer) for setting a document with authentication | ||
| */ | ||
|
Comment on lines
+44
to
+52
|
||
| default SELF document(String uri) { | ||
| ((CallOpenAPI) this.self().getTask()) | ||
| .getWith() | ||
| .withDocument( | ||
| new ExternalResource().withEndpoint(new Endpoint().withRuntimeExpression(uri))); | ||
| .setDocument(new ExternalResource().withEndpoint(EndpointUtil.fromString(uri))); | ||
| return self(); | ||
| } | ||
|
|
||
|
|
@@ -62,41 +71,33 @@ default SELF document(String uri, AuthenticationConfigurer authenticationConfigu | |
| final ReferenceableAuthenticationPolicyBuilder policy = | ||
| new ReferenceableAuthenticationPolicyBuilder(); | ||
| authenticationConfigurer.accept(policy); | ||
| ((CallOpenAPI) this.self().getTask()).getWith().setAuthentication(policy.build()); | ||
| ReferenceableAuthenticationPolicy auth = policy.build(); | ||
| ((CallOpenAPI) this.self().getTask()).getWith().setAuthentication(auth); | ||
| ((CallOpenAPI) this.self().getTask()) | ||
| .getWith() | ||
| .setDocument( | ||
| new ExternalResource() | ||
| .withEndpoint( | ||
| new Endpoint() | ||
| .withRuntimeExpression(uri) | ||
| .withEndpointConfiguration( | ||
| new EndpointConfiguration() | ||
| .withUri(new EndpointUri().withExpressionEndpointURI(uri)) | ||
| .withAuthentication(policy.build())))); | ||
| .setDocument(new ExternalResource().withEndpoint(EndpointUtil.fromString(uri, auth))); | ||
| return self(); | ||
| } | ||
|
|
||
| default SELF document(URI uri, AuthenticationConfigurer authenticationConfigurer) { | ||
| final ReferenceableAuthenticationPolicyBuilder policy = | ||
| new ReferenceableAuthenticationPolicyBuilder(); | ||
| authenticationConfigurer.accept(policy); | ||
|
|
||
| ((CallOpenAPI) this.self().getTask()).getWith().setAuthentication(policy.build()); | ||
| ReferenceableAuthenticationPolicy auth = policy.build(); | ||
| ((CallOpenAPI) this.self().getTask()).getWith().setAuthentication(auth); | ||
| ((CallOpenAPI) this.self().getTask()) | ||
| .getWith() | ||
| .setDocument( | ||
| new ExternalResource() | ||
| .withEndpoint( | ||
| new Endpoint() | ||
| .withUriTemplate(new UriTemplate().withLiteralUri(uri)) | ||
| .withEndpointConfiguration( | ||
| new EndpointConfiguration() | ||
| .withUri( | ||
| new EndpointUri() | ||
| .withLiteralEndpointURI( | ||
| new UriTemplate().withLiteralUri(uri))) | ||
| .withAuthentication(policy.build())))); | ||
| .withAuthentication(auth)))); | ||
| return self(); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one should be deleted