Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,20 @@
import io.aklivity.zilla.runtime.binding.http.config.HttpOptionsConfigBuilder;
import io.aklivity.zilla.runtime.binding.http.config.HttpRequestConfig;
import io.aklivity.zilla.runtime.binding.http.config.HttpRequestConfigBuilder;
import io.aklivity.zilla.runtime.binding.http.config.HttpResponseConfigBuilder;
import io.aklivity.zilla.runtime.binding.tcp.config.TcpConditionConfig;
import io.aklivity.zilla.runtime.binding.tcp.config.TcpOptionsConfig;
import io.aklivity.zilla.runtime.binding.tls.config.TlsOptionsConfig;
import io.aklivity.zilla.runtime.command.generate.internal.openapi.OpenApiConfigGenerator;
import io.aklivity.zilla.runtime.command.generate.internal.openapi.model.Header;
import io.aklivity.zilla.runtime.command.generate.internal.openapi.model.OpenApi;
import io.aklivity.zilla.runtime.command.generate.internal.openapi.model.Operation;
import io.aklivity.zilla.runtime.command.generate.internal.openapi.model.Parameter;
import io.aklivity.zilla.runtime.command.generate.internal.openapi.model.Response;
import io.aklivity.zilla.runtime.command.generate.internal.openapi.model.ResponseByContentType;
import io.aklivity.zilla.runtime.command.generate.internal.openapi.model.Server;
import io.aklivity.zilla.runtime.command.generate.internal.openapi.view.OperationView;
import io.aklivity.zilla.runtime.command.generate.internal.openapi.view.OperationsView;
import io.aklivity.zilla.runtime.command.generate.internal.openapi.view.PathView;
import io.aklivity.zilla.runtime.command.generate.internal.openapi.view.SchemaView;
import io.aklivity.zilla.runtime.command.generate.internal.openapi.view.ServerView;
Expand Down Expand Up @@ -202,6 +208,7 @@ private EngineConfig createConfig()
.name("http_client0")
.type("http")
.kind(CLIENT)
.inject(this::injectHttpClientOptions)
.exit(isTlsEnabled ? "tls_client0" : "tcp_client0")
.build()
.inject(this::injectTlsClient)
Expand Down Expand Up @@ -385,6 +392,104 @@ private <C> HttpRequestConfigBuilder<C> injectParams(
return request;
}

private <C> BindingConfigBuilder<C> injectHttpClientOptions(
BindingConfigBuilder<C> binding)
{
OperationsView operations = OperationsView.of(openApi.paths);
if (operations.hasResponses())
{
binding.
options(HttpOptionsConfig::builder)
.inject(options -> injectHttpClientRequests(operations, options))
.build();
}
return binding;
}

private <C> HttpOptionsConfigBuilder<C> injectHttpClientRequests(
OperationsView operations,
HttpOptionsConfigBuilder<C> options)
{
for (String pathName : openApi.paths.keySet())
{
PathView path = PathView.of(openApi.paths.get(pathName));
for (String methodName : path.methods().keySet())
{
OperationView operation = operations.operation(pathName, methodName);
if (operation.hasResponses())
{
options
.request()
.path(pathName)
.method(HttpRequestConfig.Method.valueOf(methodName))
.inject(request -> injectResponses(request, operation))
.build()
.build();
}
}
}
return options;
}

private <C> HttpRequestConfigBuilder<C> injectResponses(
HttpRequestConfigBuilder<C> request,
OperationView operation)
{
if (operation != null && operation.responsesByStatus() != null)
{
for (Map.Entry<String, ResponseByContentType> responses0 : operation.responsesByStatus().entrySet())
{
String status = responses0.getKey();
ResponseByContentType responses1 = responses0.getValue();
if (!(OperationView.DEFAULT.equals(status)) && responses1.content != null)
{
for (Map.Entry<String, Response> response2 : responses1.content.entrySet())
{
SchemaView schema = SchemaView.of(openApi.components.schemas, response2.getValue().schema);
request
.response()
.status(Integer.parseInt(status))
.contentType(response2.getKey())
.inject(response -> injectResponseHeaders(responses1, response))
.content(JsonValidatorConfig::builder)
.catalog()
.name(INLINE_CATALOG_NAME)
.schema()
.subject(schema.refKey())
.build()
.build()
.build()
.build();
}
}
}
}
return request;
}

private <C> HttpResponseConfigBuilder<C> injectResponseHeaders(
ResponseByContentType responses,
HttpResponseConfigBuilder<C> response)
{
if (responses.headers != null && !responses.headers.isEmpty())
{
for (Map.Entry<String, Header> header : responses.headers.entrySet())
{
String name = header.getKey();
ValidatorConfig validator = validators.get(header.getValue().schema.type);
if (validator != null)
{
response
.header()
.name(name)
.validator(validator)
.build();
}
}
}
return response;
}

private <C> BindingConfigBuilder<C> injectHttpServerRoutes(
BindingConfigBuilder<C> binding)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2021-2023 Aklivity Inc
*
* Licensed under the Aklivity Community License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://www.aklivity.io/aklivity-community-license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package io.aklivity.zilla.runtime.command.generate.internal.openapi.model;

public class Header
{
public Schema schema;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public class Operation
public List<Map<String, List<String>>> security;
public RequestBody requestBody;
public List<Parameter> parameters;
public Map<String, ResponseByContentType> responses;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2021-2023 Aklivity Inc
*
* Licensed under the Aklivity Community License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://www.aklivity.io/aklivity-community-license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package io.aklivity.zilla.runtime.command.generate.internal.openapi.model;

public class Response
{
public Schema schema;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2021-2023 Aklivity Inc
*
* Licensed under the Aklivity Community License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://www.aklivity.io/aklivity-community-license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package io.aklivity.zilla.runtime.command.generate.internal.openapi.model;

import java.util.LinkedHashMap;

public class ResponseByContentType
{
public LinkedHashMap<String, Header> headers;
public LinkedHashMap<String, Response> content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2021-2023 Aklivity Inc
*
* Licensed under the Aklivity Community License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://www.aklivity.io/aklivity-community-license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package io.aklivity.zilla.runtime.command.generate.internal.openapi.view;

import java.util.Map;

import io.aklivity.zilla.runtime.command.generate.internal.openapi.model.Operation;
import io.aklivity.zilla.runtime.command.generate.internal.openapi.model.ResponseByContentType;

public class OperationView
{
public static final String DEFAULT = "default";

private final Operation operation;
private final boolean hasResponses;

public OperationView(
Operation operation)
{
this.operation = operation;
this.hasResponses = initHasResponses();
}

public Map<String, ResponseByContentType> responsesByStatus()
{
return operation.responses;
}

public boolean hasResponses()
{
return hasResponses;
}

private boolean initHasResponses()
{
boolean result = false;
if (operation != null && operation.responses != null)
{
for (Map.Entry<String, ResponseByContentType> response0 : operation.responses.entrySet())
{
String status = response0.getKey();
ResponseByContentType response1 = response0.getValue();
if (!(DEFAULT.equals(status)) && response1.content != null)
{
result = true;
break;
}
}
}
return result;
}

public static OperationView of(
Operation operation)
{
return new OperationView(operation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2021-2023 Aklivity Inc
*
* Licensed under the Aklivity Community License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://www.aklivity.io/aklivity-community-license/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package io.aklivity.zilla.runtime.command.generate.internal.openapi.view;

import java.util.LinkedHashMap;
import java.util.Map;

import io.aklivity.zilla.runtime.command.generate.internal.openapi.model.PathItem;

public final class OperationsView
{
private final Map<String, Map<String, OperationView>> operationsPerPath;
private final boolean hasResponses;

private OperationsView(
LinkedHashMap<String, PathItem> paths)
{
this.operationsPerPath = new LinkedHashMap<>();
boolean hasResponses = false;
for (String pathName : paths.keySet())
{
PathView path = PathView.of(paths.get(pathName));
for (String methodName : path.methods().keySet())
{
OperationView operation = OperationView.of(path.methods().get(methodName));
hasResponses |= operation.hasResponses();
if (operationsPerPath.containsKey(pathName))
{
operationsPerPath.get(pathName).put(methodName, operation);
}
else
{
Map<String, OperationView> operationsPerMethod = new LinkedHashMap<>();
operationsPerMethod.put(methodName, operation);
operationsPerPath.put(pathName, operationsPerMethod);
}
}
}
this.hasResponses = hasResponses;
}

public boolean hasResponses()
{
return this.hasResponses;
}

public OperationView operation(
String pathName,
String methodName)
{
return operationsPerPath.get(pathName).get(methodName);
}

public static OperationsView of(
LinkedHashMap<String, PathItem> paths)
{
return new OperationsView(paths);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ paths:
'200':
description: A paged array of items
headers:
x-pages:
description: Total number of pages
schema:
type: integer
x-next:
description: A link to the next page of responses
schema:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,33 @@ bindings:
http_client0:
type: http
kind: client
options:
requests:
- path: /items
method: GET
responses:
- status: 200
content-type:
- application/json
headers:
x-pages: integer
x-next: string
content:
type: json
catalog:
catalog0:
- subject: Items
- path: "/items/{id}"
method: GET
responses:
- status: 200
content-type:
- application/json
content:
type: json
catalog:
catalog0:
- subject: Item
exit: tls_client0
tls_client0:
type: tls
Expand Down
Loading