Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,20 @@ The generator is very easy to use, and there are numeric ways to use it. Command
If you want to know all the options with the light-codegen, you can follow the tutorials for each generator at [light-codegen tutorial](https://doc.networknt.com/tutorial/generator/).

Also, there are so many [other tutorials](https://doc.networknt.com/tutorial/) that are using light-codegen to scaffold projects, and these can be found in the tutorial section.


### Kafka schema registration migration

Kafka templates use `auto.register.schemas` and the corresponding
`kafka-producer.auto.register.schemas`, `kafka-consumer.auto.register.schemas`,
and `kafka-streams.auto.register.schemas` overrides in `values.yml`.
**Before regenerating or upgrading configuration**, rename existing
`kafka-*.schema.registry.auto.register.schemas` overrides. The old override names
are unused: an old `false` override alone leaves the canonical default `true`.
Generated `values.yml` repeats this warning next to the canonical settings.
Confluent ignored the old `schema.registry.auto.register.schemas` property, so
renaming an old `false` setting makes lookup-only behavior effective. Register
schemas before selecting canonical `false`; `true` allows automatic registration.
Light's own Avro serializer retains its legacy fallback for handwritten old-key
configurations, with the canonical key taking precedence. Users relying on that
fallback must also migrate their values overrides to retain a `false` setting.
2 changes: 1 addition & 1 deletion codegen-web/src/main/resources/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ action:
scope: codegen.w
request:
schema:
$ref: '#/schemas/singleRequest'
$ref: '#/schemas/singleRequest'
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public void generate(String targetPath, Object model, JsonNode config) throws IO
// added with #471
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "app-status.yml", templates.hybrid.appStatusYml.template());
// values.yml file, transfer to suppress the warning message during start startup and encourage usage.
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "values.yml", templates.hybrid.values.template(config, handlerPackage, jsonPath, serviceId, enableHttp, httpPort, enableHttps, httpsPort, enableHttp2, enableRegistry, version));
transfer(targetPath, ("src.test.resources.config").replace(".", separator), "values.yml", templates.hybrid.values.template(config, handlerPackage, jsonPath, serviceId, enableHttp, "49587", enableHttps, "49588", enableHttp2, enableRegistry, version));
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "values.yml", templates.hybrid.values.template(config, handlerPackage, jsonPath, serviceId, enableHttp, httpPort, enableHttps, httpsPort, enableHttp2, enableRegistry, version, false, kafkaConsumer));
transfer(targetPath, ("src.test.resources.config").replace(".", separator), "values.yml", templates.hybrid.values.template(config, handlerPackage, jsonPath, serviceId, enableHttp, "49587", enableHttps, "49588", enableHttp2, enableRegistry, version, false, kafkaConsumer));

// transfer binary files without touching them.
try (InputStream is = HybridServerGenerator.class.getResourceAsStream("/binaries/server.keystore")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void generate(String targetPath, Object model, JsonNode config) throws IO
// added with #471
transfer(targetPath, ("src.test.resources.config").replace(".", separator), "app-status.yml", templates.hybrid.appStatusYml.template());
// values.yml file, transfer to suppress the warning message during start startup and encourage usage.
transfer(targetPath, ("src.test.resources.config").replace(".", separator), "values.yml", templates.hybrid.values.template(config, handlerPackage, jsonPath, serviceId, enableHttp, "49587", enableHttps, "49588", enableHttp2, enableRegistry, version));
transfer(targetPath, ("src.test.resources.config").replace(".", separator), "values.yml", templates.hybrid.values.template(config, handlerPackage, jsonPath, serviceId, enableHttp, "49587", enableHttps, "49588", enableHttp2, enableRegistry, version, kafkaConsumer, kafkaStreams));

// handler
JsonNode anyModel = (JsonNode)model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ properties:
group.id: ${kafka-consumer.group.id:group1}
# Schema registry url
schema.registry.url: ${kafka-consumer.schema.registry.url:http://localhost:8081}
# Schema registry auto register schema indicator for streams application. If true, the first request will register the schema auto automatically.
schema.registry.auto.register.schemas: ${kafka-consumer.schema.registry.auto.register.schemas:true}
# Automatically register schemas during serialization. False requires pre-registered schemas.
auto.register.schemas: ${kafka-consumer.auto.register.schemas:true}
# Schema registry client truststore location, use the following two properties only if schema registry url is https.
# schema.registry.ssl.truststore.location: ${kafka-consumer.schema.registry.ssl.truststore.location:/config/client.truststore}
# Schema registry client truststore password
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ properties:
schema.registry.url: ${kafka-producer.schema.registry.url:http://localhost:8081}
# Schema registry identity cache size
schema.registry.cache: ${kafka-producer.schema.registry.cache:100}
# Schema registry auto register schema indicator for streams application. If true, the first request will register the schema auto automatically.
schema.registry.auto.register.schemas: ${kafka-producer.schema.registry.auto.register.schemas:true}
# Automatically register schemas during serialization. False requires pre-registered schemas.
auto.register.schemas: ${kafka-producer.auto.register.schemas:true}
# Schema registry client truststore location, use the following two properties only if schema registry url is https.
# schema.registry.ssl.truststore.location: ${kafka-producer.schema.registry.ssl.truststore.location:/config/client.truststore}
# Schema registry client truststore password
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ properties:
application.id: ${kafka-streams.application.id:@artifactId}
# Schema registry url
schema.registry.url: ${kafka-streams.schema.registry.url:http://localhost:8081}
# Schema registry auto register schema indicator for streams application. If true, the first request will register the schema auto automatically.
schema.registry.auto.register.schemas: ${kafka-streams.schema.registry.auto.register.schemas:true}
# Automatically register schemas during serialization. False requires pre-registered schemas.
auto.register.schemas: ${kafka-streams.auto.register.schemas:true}
# Schema registry client truststore location, use the following two properties only if schema registry url is https.
# schema.registry.ssl.truststore.location: ${kafka-streams.schema.registry.ssl.truststore.location:/config/client.truststore}
# Schema registry client truststore password
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@import com.fasterxml.jackson.databind.JsonNode
@args (JsonNode config, String handlerPackage, String jsonPath, String serviceId, Boolean enableHttp, String httpPort, Boolean enableHttps, String httpsPort, Boolean enableHttp2, Boolean enableRegistry, String version)
@args (JsonNode config, String handlerPackage, String jsonPath, String serviceId, Boolean enableHttp, String httpPort, Boolean enableHttps, String httpsPort, Boolean enableHttp2, Boolean enableRegistry, String version, Boolean generateKafkaConsumerConfig, Boolean generateKafkaStreamsConfig)
#--------------------------------------------------------------------------------
# values.yml : Set of values commonly overridden in microservices
#--------------------------------------------------------------------------------
Expand Down Expand Up @@ -64,3 +64,16 @@ service.singletons:
- io.confluent.kafka.schemaregistry.client.SchemaRegistryClient:
- com.networknt.kafka.common.LightSchemaRegistryClient
}

# Schema registration: false requires pre-registered schemas.
# When upgrading, rename kafka-*.schema.registry.auto.register.schemas overrides.
# Old override names are unused; an old false alone leaves the true default below.
@if(config.get("kafkaProducer").booleanValue()){
kafka-producer.auto.register.schemas: true
}
@if(generateKafkaConsumerConfig){
kafka-consumer.auto.register.schemas: true
}
@if(generateKafkaStreamsConfig){
kafka-streams.auto.register.schemas: true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.networknt.codegen.hybrid;

import com.networknt.codegen.Generator;
import com.networknt.config.ConfigInjection;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;

class KafkaSchemaRegistrationGenerationTest {
@TempDir Path output;

@Test
void serverAndServiceEmitCanonicalKafkaSettingsAndValues() throws Exception {
for (int flags = 0; flags < 8; flags++) {
boolean producer = (flags & 1) != 0;
boolean consumer = (flags & 2) != 0;
boolean streams = (flags & 4) != 0;
ObjectNode server = (ObjectNode) Generator.jsonMapper.readTree(
getClass().getResourceAsStream("/serverConfig.json"));
ObjectNode service = (ObjectNode) Generator.yamlMapper.readTree(
getClass().getResourceAsStream("/service.yaml"));
for (ObjectNode config : List.of(server, service)) {
config.put("kafkaProducer", producer);
config.put("kafkaConsumer", consumer);
config.put("kafkaStreams", streams);
}
Path serverOutput = output.resolve("server-" + flags);
Path serviceOutput = output.resolve("service-" + flags);
new HybridServerGenerator().generate(serverOutput.toString(), null, server);
new HybridServiceGenerator().generate(serviceOutput.toString(),
Generator.yamlMapper.readTree(getClass().getResourceAsStream("/spec.yaml")), service);

// Preserve the existing server emission contract: its consumer flag emits Streams.
Set<String> serverClients = new HashSet<>();
if (producer) serverClients.add("kafka-producer");
if (consumer) serverClients.add("kafka-streams");
Set<String> serviceClients = new HashSet<>();
if (producer) serviceClients.add("kafka-producer");
if (consumer) serviceClients.add("kafka-consumer");
if (streams) serviceClients.add("kafka-streams");
assertConfigsAndValues(serverOutput.resolve("src/main/resources/config"), serverClients);
assertConfigsAndValues(serviceOutput.resolve("src/test/resources/config"), serviceClients);
assertValues(serverOutput.resolve("src/test/resources/config/values.yml"), serverClients);
}
}

private void assertConfigsAndValues(Path directory, Set<String> expectedClients) throws Exception {
assertValues(directory.resolve("values.yml"), expectedClients);
Map<String, Object> original = ConfigInjection.getDecryptedValueMap();
try {
for (String client : List.of("kafka-producer", "kafka-consumer", "kafka-streams")) {
Path path = directory.resolve(client + ".yml");
assertEquals(expectedClients.contains(client), Files.exists(path), path.toString());
if (!expectedClients.contains(client)) continue;
String yaml = Files.readString(path);
String placeholder = "${" + client + ".auto.register.schemas:true}";
assertTrue(yaml.contains("auto.register.schemas: " + placeholder), path.toString());
assertFalse(yaml.contains("schema.registry.auto.register.schemas"), path.toString());
ConfigInjection.setDecryptedValueMap(new HashMap<>(Map.of(client + ".schema.registry.auto.register.schemas", false)));
assertEquals(true, ConfigInjection.getInjectValue(placeholder, true), "Old names require explicit migration");
ConfigInjection.setDecryptedValueMap(new HashMap<>(Map.of(client + ".auto.register.schemas", false)));
assertEquals(false, ConfigInjection.getInjectValue(placeholder, true), path.toString());
}
} finally {
ConfigInjection.setDecryptedValueMap(original);
}
}

private void assertValues(Path path, Set<String> expectedClients) throws Exception {
String values = Files.readString(path);
for (String client : List.of("kafka-producer", "kafka-consumer", "kafka-streams")) {
assertEquals(expectedClients.contains(client), values.contains(client + ".auto.register.schemas: true"),
path + " " + client);
}
}
}