diff --git a/README.md b/README.md index 3e249f4a..a66970e9 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/codegen-web/src/main/resources/spec.yaml b/codegen-web/src/main/resources/spec.yaml index 5d7b9aba..64a162ca 100644 --- a/codegen-web/src/main/resources/spec.yaml +++ b/codegen-web/src/main/resources/spec.yaml @@ -119,4 +119,4 @@ action: scope: codegen.w request: schema: - $ref: '#/schemas/singleRequest' \ No newline at end of file + $ref: '#/schemas/singleRequest' diff --git a/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServerGenerator.java b/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServerGenerator.java index 1cb3428e..442cfd04 100644 --- a/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServerGenerator.java +++ b/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServerGenerator.java @@ -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")) { diff --git a/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServiceGenerator.java b/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServiceGenerator.java index 1f74501f..5bc5c180 100644 --- a/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServiceGenerator.java +++ b/light-hybrid-4j/src/main/java/com/networknt/codegen/hybrid/HybridServiceGenerator.java @@ -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; diff --git a/light-hybrid-4j/src/main/resources/templates/hybrid/kafkaConsumerYml.rocker.raw b/light-hybrid-4j/src/main/resources/templates/hybrid/kafkaConsumerYml.rocker.raw index 94c3e27d..8e810018 100644 --- a/light-hybrid-4j/src/main/resources/templates/hybrid/kafkaConsumerYml.rocker.raw +++ b/light-hybrid-4j/src/main/resources/templates/hybrid/kafkaConsumerYml.rocker.raw @@ -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 diff --git a/light-hybrid-4j/src/main/resources/templates/hybrid/kafkaProducerYml.rocker.raw b/light-hybrid-4j/src/main/resources/templates/hybrid/kafkaProducerYml.rocker.raw index 93315c71..bfe5b99c 100644 --- a/light-hybrid-4j/src/main/resources/templates/hybrid/kafkaProducerYml.rocker.raw +++ b/light-hybrid-4j/src/main/resources/templates/hybrid/kafkaProducerYml.rocker.raw @@ -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 diff --git a/light-hybrid-4j/src/main/resources/templates/hybrid/kafkaStreamsYml.rocker.raw b/light-hybrid-4j/src/main/resources/templates/hybrid/kafkaStreamsYml.rocker.raw index ef6a59fe..bcb0d454 100644 --- a/light-hybrid-4j/src/main/resources/templates/hybrid/kafkaStreamsYml.rocker.raw +++ b/light-hybrid-4j/src/main/resources/templates/hybrid/kafkaStreamsYml.rocker.raw @@ -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 diff --git a/light-hybrid-4j/src/main/resources/templates/hybrid/values.rocker.raw b/light-hybrid-4j/src/main/resources/templates/hybrid/values.rocker.raw index fad8fe25..f3944243 100644 --- a/light-hybrid-4j/src/main/resources/templates/hybrid/values.rocker.raw +++ b/light-hybrid-4j/src/main/resources/templates/hybrid/values.rocker.raw @@ -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 #-------------------------------------------------------------------------------- @@ -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 +} diff --git a/light-hybrid-4j/src/test/java/com/networknt/codegen/hybrid/KafkaSchemaRegistrationGenerationTest.java b/light-hybrid-4j/src/test/java/com/networknt/codegen/hybrid/KafkaSchemaRegistrationGenerationTest.java new file mode 100644 index 00000000..767a3467 --- /dev/null +++ b/light-hybrid-4j/src/test/java/com/networknt/codegen/hybrid/KafkaSchemaRegistrationGenerationTest.java @@ -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 serverClients = new HashSet<>(); + if (producer) serverClients.add("kafka-producer"); + if (consumer) serverClients.add("kafka-streams"); + Set 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 expectedClients) throws Exception { + assertValues(directory.resolve("values.yml"), expectedClients); + Map 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 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); + } + } +}