Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
WIP generate openapi
  • Loading branch information
attilakreiner committed Jan 17, 2024
commit fc7f5c0978c47250e10fc8b1e6f0131b32046e38
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public abstract class ConfigGenerator
protected static final String APPLICATION_JSON = "application/json";
protected static final String VERSION_LATEST = "latest";
protected static final Pattern JSON_CONTENT_TYPE = Pattern.compile("^application/(?:.+\\+)?json$");
protected static final String DEFAULT = "default";

protected final Map<String, ValidatorConfig> validators = Map.of(
"string", StringValidatorConfig.builder().build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
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.PathView;
import io.aklivity.zilla.runtime.command.generate.internal.openapi.view.SchemaView;
Expand Down Expand Up @@ -202,6 +204,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 +388,117 @@ private <C> HttpRequestConfigBuilder<C> injectParams(
return request;
}

private <C> BindingConfigBuilder<C> injectHttpClientOptions(
BindingConfigBuilder<C> binding)
{
if (hasResponses())
{
binding.
options(HttpOptionsConfig::builder)
.inject(this::injectHttpClientRequests)
.build();
}
return binding;
}

private boolean hasResponses()
{
boolean result = false;
for (String pathName : openApi.paths.keySet())
{
PathView path = PathView.of(openApi.paths.get(pathName));
for (String methodName : path.methods().keySet())
{
Operation operation = path.methods().get(methodName);
if (hasResponses(operation))
{
result = true;
break;
}
}
}
return result;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method feels like it should be on OperationView instead of here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I managed to move both hasRepsonses methods out from here. It was necessary to create an OperationView and an OperationsView class (singular and plural). I think it makes sense, pls chk.


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

private boolean hasResponses(
Operation operation)
{
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;
}

private <C> HttpRequestConfigBuilder<C> injectResponses(
HttpRequestConfigBuilder<C> request,
Operation operation)
{
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)
{
for (Map.Entry<String, Response> response2 : response1.content.entrySet())
{
SchemaView schema = SchemaView.of(openApi.components.schemas, response2.getValue().schema);
request
.response()
.status(Integer.parseInt(status))
.contentType(response2.getKey())
.content(JsonValidatorConfig::builder)
.catalog()
.name(INLINE_CATALOG_NAME)
.schema()
.subject(schema.refKey())
.build()
.build()
.build()
.build();
}
}
}
}
return request;
}

private <C> BindingConfigBuilder<C> injectHttpServerRoutes(
BindingConfigBuilder<C> binding)
{
Expand Down
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; // responses by status code
Comment thread
attilakreiner marked this conversation as resolved.
Outdated
}
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,22 @@
/*
* 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.Map;

public class ResponseByContentType
{
public Map<String, Response> content;
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ bindings:
http_client0:
type: http
kind: client
options:
requests:
- path: /items
method: GET
responses:
- status: 200
content-type:
- application/json
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@ paths:
responses:
'200':
description: A paged array of items
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: "#/components/schemas/Items"
Comment thread
attilakreiner marked this conversation as resolved.
default:
description: unexpected error
content:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ paths:
responses:
'200':
description: A paged array of items
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: "#/components/schemas/Items"
Comment thread
attilakreiner marked this conversation as resolved.
default:
description: unexpected error
content:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@ paths:
responses:
'200':
description: A paged array of items
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: "#/components/schemas/Items"
Comment thread
attilakreiner marked this conversation as resolved.
default:
description: unexpected error
content:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ bindings:
http_client0:
type: http
kind: client
options:
requests:
- path: /items
method: GET
responses:
- status: 200
content-type:
- application/json
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: tcp_client0
tcp_client0:
type: tcp
Expand Down