Skip to content
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

Fragment validator interface & implementation #735

Merged
merged 11 commits into from
Jan 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;

import io.aklivity.zilla.runtime.engine.config.ConverterConfig;
import io.aklivity.zilla.runtime.types.core.config.IntegerConverterConfig;
import io.aklivity.zilla.runtime.types.core.config.StringConverterConfig;
import io.aklivity.zilla.runtime.engine.config.ValidatorConfig;
import io.aklivity.zilla.runtime.types.core.config.IntegerValidatorConfig;
import io.aklivity.zilla.runtime.types.core.config.StringValidatorConfig;

public abstract class ConfigGenerator
{
Expand All @@ -40,9 +40,9 @@ public abstract class ConfigGenerator
protected static final String VERSION_LATEST = "latest";
protected static final Pattern JSON_CONTENT_TYPE = Pattern.compile("^application/(?:.+\\+)?json$");

protected final Map<String, ConverterConfig> converters = Map.of(
"string", StringConverterConfig.builder().build(),
"integer", IntegerConverterConfig.builder().build()
protected final Map<String, ValidatorConfig> validators = Map.of(
"string", StringValidatorConfig.builder().build(),
"integer", IntegerValidatorConfig.builder().build()
);
protected final Matcher jsonContentType = JSON_CONTENT_TYPE.matcher("");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@
import io.aklivity.zilla.runtime.engine.config.BindingConfigBuilder;
import io.aklivity.zilla.runtime.engine.config.CatalogedConfigBuilder;
import io.aklivity.zilla.runtime.engine.config.ConfigWriter;
import io.aklivity.zilla.runtime.engine.config.ConverterConfig;
import io.aklivity.zilla.runtime.engine.config.GuardedConfigBuilder;
import io.aklivity.zilla.runtime.engine.config.NamespaceConfig;
import io.aklivity.zilla.runtime.engine.config.NamespaceConfigBuilder;
import io.aklivity.zilla.runtime.engine.config.RouteConfigBuilder;
import io.aklivity.zilla.runtime.engine.config.ValidatorConfig;
import io.aklivity.zilla.runtime.guard.jwt.config.JwtOptionsConfig;
import io.aklivity.zilla.runtime.types.json.config.JsonConverterConfig;
import io.aklivity.zilla.runtime.types.json.config.JsonValidatorConfig;
import io.aklivity.zilla.runtime.vault.filesystem.config.FileSystemOptionsConfig;

public class AsyncApiHttpProxyConfigGenerator extends AsyncApiConfigGenerator
Expand Down Expand Up @@ -355,7 +355,7 @@ private <C> HttpRequestConfigBuilder<C> injectContent(
if (hasJsonContentType())
{
request.
content(JsonConverterConfig::builder)
content(JsonValidatorConfig::builder)
.catalog()
.name(INLINE_CATALOG_NAME)
.inject(catalog -> injectSchemas(catalog, messages))
Expand Down Expand Up @@ -394,13 +394,13 @@ private <C> HttpRequestConfigBuilder<C> injectPathParams(
Parameter parameter = parameters.get(name);
if (parameter.schema != null && parameter.schema.type != null)
{
ConverterConfig converter = converters.get(parameter.schema.type);
if (converter != null)
ValidatorConfig validator = validators.get(parameter.schema.type);
if (validator != null)
{
request
.pathParam()
.name(name)
.converter(converter)
.validator(validator)
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import io.aklivity.zilla.runtime.engine.config.ConfigWriter;
import io.aklivity.zilla.runtime.engine.config.NamespaceConfig;
import io.aklivity.zilla.runtime.engine.config.NamespaceConfigBuilder;
import io.aklivity.zilla.runtime.types.json.config.JsonConverterConfig;
import io.aklivity.zilla.runtime.types.json.config.JsonValidatorConfig;
import io.aklivity.zilla.runtime.vault.filesystem.config.FileSystemOptionsConfig;

public class AsyncApiMqttProxyConfigGenerator extends AsyncApiConfigGenerator
Expand Down Expand Up @@ -248,7 +248,7 @@ private BindingConfigBuilder<NamespaceConfigBuilder<NamespaceConfig>> injectMqtt
.options(MqttOptionsConfig::builder)
.topic()
.name(topic)
.content(JsonConverterConfig::builder)
.content(JsonValidatorConfig::builder)
.catalog()
.name(INLINE_CATALOG_NAME)
.inject(cataloged -> injectJsonSchemas(cataloged, messages, APPLICATION_JSON))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
import io.aklivity.zilla.runtime.command.generate.internal.openapi.view.ServerView;
import io.aklivity.zilla.runtime.engine.config.BindingConfigBuilder;
import io.aklivity.zilla.runtime.engine.config.ConfigWriter;
import io.aklivity.zilla.runtime.engine.config.ConverterConfig;
import io.aklivity.zilla.runtime.engine.config.GuardedConfigBuilder;
import io.aklivity.zilla.runtime.engine.config.NamespaceConfig;
import io.aklivity.zilla.runtime.engine.config.NamespaceConfigBuilder;
import io.aklivity.zilla.runtime.engine.config.RouteConfigBuilder;
import io.aklivity.zilla.runtime.engine.config.ValidatorConfig;
import io.aklivity.zilla.runtime.guard.jwt.config.JwtOptionsConfig;
import io.aklivity.zilla.runtime.types.json.config.JsonConverterConfig;
import io.aklivity.zilla.runtime.types.json.config.JsonValidatorConfig;
import io.aklivity.zilla.runtime.vault.filesystem.config.FileSystemOptionsConfig;

public class OpenApiHttpProxyConfigGenerator extends OpenApiConfigGenerator
Expand Down Expand Up @@ -326,7 +326,7 @@ private <C> HttpRequestConfigBuilder<C> injectContent(
if (schema != null)
{
request.
content(JsonConverterConfig::builder)
content(JsonValidatorConfig::builder)
.catalog()
.name(INLINE_CATALOG_NAME)
.schema()
Expand All @@ -349,30 +349,30 @@ private <C> HttpRequestConfigBuilder<C> injectParams(
{
if (parameter.schema != null && parameter.schema.type != null)
{
ConverterConfig converter = converters.get(parameter.schema.type);
if (converter != null)
ValidatorConfig validator = validators.get(parameter.schema.type);
if (validator != null)
{
switch (parameter.in)
{
case "path":
request.
pathParam()
.name(parameter.name)
.converter(converter)
.validator(validator)
.build();
break;
case "query":
request.
queryParam()
.name(parameter.name)
.converter(converter)
.validator(validator)
.build();
break;
case "header":
request.
header()
.name(parameter.name)
.converter(converter)
.validator(validator)
.build();
break;
}
Expand Down
2 changes: 1 addition & 1 deletion incubator/types-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<jacoco.coverage.ratio>0.80</jacoco.coverage.ratio>
<jacoco.coverage.ratio>0.84</jacoco.coverage.ratio>
<jacoco.missed.count>0</jacoco.missed.count>
</properties>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.types.core;

import java.net.URL;

import io.aklivity.zilla.runtime.engine.EngineContext;
import io.aklivity.zilla.runtime.engine.validator.Validator;
import io.aklivity.zilla.runtime.engine.validator.ValidatorContext;

public class IntegerValidator implements Validator
{
public static final String NAME = "integer";

@Override
public String name()
{
return IntegerValidator.NAME;
}

@Override
public ValidatorContext supply(
EngineContext context)
{
return new IntegerValidatorContext(context);
}

@Override
public URL type()
{
return getClass().getResource("schema/integer.schema.patch.json");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.types.core;

import io.aklivity.zilla.runtime.engine.EngineContext;
import io.aklivity.zilla.runtime.engine.config.ValidatorConfig;
import io.aklivity.zilla.runtime.engine.validator.ValidatorContext;
import io.aklivity.zilla.runtime.engine.validator.ValidatorHandler;
import io.aklivity.zilla.runtime.types.core.config.IntegerValidatorConfig;

public class IntegerValidatorContext implements ValidatorContext
{
public IntegerValidatorContext(
EngineContext context)
{
}

@Override
public ValidatorHandler supplyHandler(
ValidatorConfig config)
{
return new IntegerValidatorHandler(IntegerValidatorConfig.class.cast(config));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.types.core;

import io.aklivity.zilla.runtime.engine.Configuration;
import io.aklivity.zilla.runtime.engine.validator.Validator;
import io.aklivity.zilla.runtime.engine.validator.ValidatorFactorySpi;

public class IntegerValidatorFactorySpi implements ValidatorFactorySpi
{
@Override
public String type()
{
return IntegerValidator.NAME;
}

@Override
public Validator create(
Configuration config)
{
return new IntegerValidator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.types.core;

import org.agrona.DirectBuffer;

import io.aklivity.zilla.runtime.engine.converter.function.ValueConsumer;
import io.aklivity.zilla.runtime.engine.validator.ValidatorHandler;
import io.aklivity.zilla.runtime.types.core.config.IntegerValidatorConfig;

public class IntegerValidatorHandler implements ValidatorHandler
{
private int pendingBytes;

public IntegerValidatorHandler(
IntegerValidatorConfig config)
{
}

@Override
public boolean validate(
int flags,
DirectBuffer data,
int index,
int length,
ValueConsumer next)
{
boolean valid;

if ((flags & FLAGS_INIT) != 0x00)
{
pendingBytes = 4;
}

pendingBytes = pendingBytes - length;

if ((flags & FLAGS_FIN) != 0x00)
{
valid = pendingBytes == 0;
}
else
{
valid = pendingBytes >= 0;
}
return valid;
}
}
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.types.core;

import java.net.URL;

import io.aklivity.zilla.runtime.engine.EngineContext;
import io.aklivity.zilla.runtime.engine.validator.Validator;
import io.aklivity.zilla.runtime.engine.validator.ValidatorContext;

public class StringValidator implements Validator
{
public static final String NAME = "string";

public StringValidator()
{
}

@Override
public String name()
{
return StringValidator.NAME;
}

@Override
public ValidatorContext supply(
EngineContext context)
{
return new StringValidatorContext(context);
}

@Override
public URL type()
{
return getClass().getResource("schema/string.schema.patch.json");
}
}
Loading