Skip to content

Commit

Permalink
Support multiple specs in openapi binding (#1005)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmaidics authored May 8, 2024
1 parent c1bdba2 commit 640c334
Show file tree
Hide file tree
Showing 19 changed files with 1,108 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.function.ToLongFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.agrona.AsciiSequenceView;
import org.agrona.DirectBuffer;
Expand All @@ -47,6 +48,7 @@
import io.aklivity.zilla.runtime.binding.openapi.internal.types.String16FW;
import io.aklivity.zilla.runtime.binding.openapi.internal.types.String8FW;
import io.aklivity.zilla.runtime.binding.openapi.internal.types.stream.HttpBeginExFW;
import io.aklivity.zilla.runtime.binding.openapi.internal.view.OpenapiServerView;
import io.aklivity.zilla.runtime.engine.catalog.CatalogHandler;
import io.aklivity.zilla.runtime.engine.config.BindingConfig;
import io.aklivity.zilla.runtime.engine.config.KindConfig;
Expand Down Expand Up @@ -119,21 +121,39 @@ public void attach(
BindingConfig binding)
{
List<OpenapiSchemaConfig> configs = convertToOpenapi(options.openapis);
configs.forEach(c ->

final Map<Integer, OpenapiNamespaceConfig> namespaceConfigs = new HashMap<>();
for (OpenapiSchemaConfig config : configs)
{
Openapi openapi = config.openapi;
final List<OpenapiServerView> servers =
namespaceGenerator.filterOpenapiServers(openapi.servers, options.openapis.stream()
.flatMap(o -> o.servers.stream())
.collect(Collectors.toList()));

servers.stream().collect(Collectors.groupingBy(OpenapiServerView::getPort)).forEach((k, v) ->
namespaceConfigs.computeIfAbsent(k, s -> new OpenapiNamespaceConfig()).addSpecForNamespace(v, config, openapi));
}

for (OpenapiNamespaceConfig namespaceConfig : namespaceConfigs.values())
{
Openapi openapi = c.openapi;
final NamespaceConfig composite = namespaceGenerator.generate(binding, openapi);
final NamespaceConfig composite = namespaceGenerator.generate(binding, namespaceConfig);
composite.readURL = binding.readURL;
attach.accept(composite);
composites.put(c.schemaId, composite);
openapi.paths.forEach((k, v) ->
namespaceConfig.configs.forEach(c ->
{
String regex = k.replaceAll("\\{[^/]+}", "[^/]+");
regex = "^" + regex + "$";
Pattern pattern = Pattern.compile(regex);
paths.put(pattern.matcher(""), v);
composites.put(c.schemaId, composite);
namespaceConfig.openapis.forEach(o ->
o.paths.forEach((k, v) ->
{
String regex = k.replaceAll("\\{[^/]+}", "[^/]+");
regex = "^" + regex + "$";
Pattern pattern = Pattern.compile(regex);
paths.put(pattern.matcher(""), v);
})
);
});
});
}

composites.forEach((k, v) ->
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.aklivity.zilla.runtime.binding.http.config.HttpOptionsConfig;
import io.aklivity.zilla.runtime.binding.http.config.HttpOptionsConfigBuilder;
Expand Down Expand Up @@ -55,16 +54,12 @@ public final class OpenapiClientNamespaceGenerator extends OpenapiNamespaceGener
@Override
public NamespaceConfig generate(
BindingConfig binding,
Openapi openapi)
OpenapiNamespaceConfig namespaceConfig)
{
final OpenapiOptionsConfig options = (OpenapiOptionsConfig) binding.options;
final List<MetricRefConfig> metricRefs = binding.telemetryRef != null ?
binding.telemetryRef.metricRefs : emptyList();
final List<OpenapiServerView> servers =
filterOpenapiServers(
openapi.servers, options.openapis.stream()
.flatMap(o -> o.servers.stream())
.collect(Collectors.toList()));
List<OpenapiServerView> servers = namespaceConfig.servers;

final int[] httpsPorts = resolvePortsForScheme("https", servers);
final boolean secure = httpsPorts.length != 0;
Expand All @@ -76,7 +71,7 @@ public NamespaceConfig generate(
.name("http_client0")
.type("http")
.kind(CLIENT)
.inject(b -> this.injectHttpClientOptions(b, openapi))
.inject(b -> this.injectHttpClientOptions(b, namespaceConfig.openapis))
.inject(b -> this.injectMetrics(b, metricRefs, "http"))
.exit(secure ? "tls_client0" : "tcp_client0")
.build()
Expand All @@ -93,15 +88,18 @@ public NamespaceConfig generate(

private <C> BindingConfigBuilder<C> injectHttpClientOptions(
BindingConfigBuilder<C> binding,
Openapi openApi)
List<Openapi> openApis)
{
OpenapiOperationsView operations = OpenapiOperationsView.of(openApi.paths);
if (operations.hasResponses())
for (Openapi openApi : openApis)
{
binding.
options(HttpOptionsConfig::builder)
.inject(options -> injectHttpClientRequests(operations, options, openApi))
.build();
OpenapiOperationsView operations = OpenapiOperationsView.of(openApi.paths);
if (operations.hasResponses())
{
binding.
options(HttpOptionsConfig::builder)
.inject(options -> injectHttpClientRequests(operations, options, openApi))
.build();
}
}
return binding;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.binding.openapi.internal.config;

import java.util.ArrayList;
import java.util.List;

import io.aklivity.zilla.runtime.binding.openapi.config.OpenapiSchemaConfig;
import io.aklivity.zilla.runtime.binding.openapi.internal.model.Openapi;
import io.aklivity.zilla.runtime.binding.openapi.internal.view.OpenapiServerView;

public class OpenapiNamespaceConfig
{
List<String> openapiLabels;
List<OpenapiServerView> servers;
List<Openapi> openapis;
List<OpenapiSchemaConfig> configs;

public OpenapiNamespaceConfig()
{
openapiLabels = new ArrayList<>();
servers = new ArrayList<>();
openapis = new ArrayList<>();
configs = new ArrayList<>();
}

public void addSpecForNamespace(
List<OpenapiServerView> servers,
OpenapiSchemaConfig config,
Openapi openapi)
{
this.openapiLabels.add(config.apiLabel);
this.servers.addAll(servers);
this.configs.add(config);
this.openapis.add(openapi);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import io.aklivity.zilla.runtime.binding.openapi.config.OpenapiOptionsConfig;
import io.aklivity.zilla.runtime.binding.openapi.config.OpenapiServerConfig;
import io.aklivity.zilla.runtime.binding.openapi.internal.model.Openapi;
import io.aklivity.zilla.runtime.binding.openapi.internal.model.OpenapiServer;
import io.aklivity.zilla.runtime.binding.openapi.internal.view.OpenapiServerView;
import io.aklivity.zilla.runtime.engine.config.BindingConfig;
Expand Down Expand Up @@ -63,7 +62,7 @@ public abstract class OpenapiNamespaceGenerator

public abstract NamespaceConfig generate(
BindingConfig binding,
Openapi openapi);
OpenapiNamespaceConfig namespaceConfig);

protected <C> NamespaceConfigBuilder<C> injectNamespaceMetric(
NamespaceConfigBuilder<C> namespace,
Expand Down
Loading

0 comments on commit 640c334

Please sign in to comment.