From 9ec6148c548efbaa502ab5ed4cbdb45e093d46a6 Mon Sep 17 00:00:00 2001 From: Shivam Gupta Date: Wed, 9 Oct 2024 16:25:43 +0530 Subject: [PATCH] Fix(catalog): Expressions does not show input expression field as required --- .../generator/CamelCatalogProcessor.java | 19 +++++++++++ .../generator/CamelCatalogProcessorTest.java | 34 ++++++++++++++----- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/packages/catalog-generator/src/main/java/io/kaoto/camelcatalog/generator/CamelCatalogProcessor.java b/packages/catalog-generator/src/main/java/io/kaoto/camelcatalog/generator/CamelCatalogProcessor.java index d0efb4421..5256bad0f 100644 --- a/packages/catalog-generator/src/main/java/io/kaoto/camelcatalog/generator/CamelCatalogProcessor.java +++ b/packages/catalog-generator/src/main/java/io/kaoto/camelcatalog/generator/CamelCatalogProcessor.java @@ -226,6 +226,8 @@ public String getDataFormatCatalog() throws Exception { var json = JsonMapper.asJsonObject(dataFormatCatalog).toJson(); var catalogTree = (ObjectNode) jsonMapper.readTree(json); catalogTree.set("propertiesSchema", dataFormatSchema); + // setting required property to all the dataformats schema + setRequiredToPropertiesSchema(dataFormatSchema, catalogTree); answer.set(dataFormatName, catalogTree); } StringWriter writer = new StringWriter(); @@ -267,6 +269,8 @@ public String getLanguageCatalog() throws Exception { var json = JsonMapper.asJsonObject(languageCatalog).toJson(); var catalogTree = (ObjectNode) jsonMapper.readTree(json); catalogTree.set("propertiesSchema", languageSchema); + // setting required property to all the languages schema + setRequiredToPropertiesSchema(languageSchema, catalogTree); answer.set(languageName, catalogTree); } StringWriter writer = new StringWriter(); @@ -709,6 +713,8 @@ public String getLoadBalancerCatalog() throws Exception { var json = JsonMapper.asJsonObject(loadBalancerCatalog).toJson(); var catalogTree = (ObjectNode) jsonMapper.readTree(json); catalogTree.set("propertiesSchema", loadBalancerSchema); + // setting required property to all the load-balancers schema + setRequiredToPropertiesSchema(loadBalancerSchema, catalogTree); answer.set(loadBalancerName, catalogTree); } StringWriter writer = new StringWriter(); @@ -716,4 +722,17 @@ public String getLoadBalancerCatalog() throws Exception { jsonMapper.writeTree(jsonGenerator, answer); return writer.toString(); } + + private void setRequiredToPropertiesSchema(ObjectNode camelYamlDslSchema, ObjectNode catalogModel) { + List required = new ArrayList<>(); + var camelYamlDslProperties = camelYamlDslSchema.withObject("/properties").properties().stream() + .map(Map.Entry::getKey).toList(); + for (var propertyName : camelYamlDslProperties) { + var catalogPropertySchema = catalogModel.withObject("/properties").withObject("/" + propertyName); + if (catalogPropertySchema.has("required") && catalogPropertySchema.get("required").asBoolean()) { + required.add(propertyName); + } + } + catalogModel.withObject("/propertiesSchema").set("required", jsonMapper.valueToTree(required)); + } } diff --git a/packages/catalog-generator/src/test/java/io/kaoto/camelcatalog/generator/CamelCatalogProcessorTest.java b/packages/catalog-generator/src/test/java/io/kaoto/camelcatalog/generator/CamelCatalogProcessorTest.java index 91af49402..8f21e454c 100644 --- a/packages/catalog-generator/src/test/java/io/kaoto/camelcatalog/generator/CamelCatalogProcessorTest.java +++ b/packages/catalog-generator/src/test/java/io/kaoto/camelcatalog/generator/CamelCatalogProcessorTest.java @@ -170,6 +170,9 @@ public void testGetDataFormatCatalog() throws Exception { assertEquals("Custom", customPropertiesSchema.get("title").asText()); var refProperty = customPropertiesSchema.withObject("/properties").withObject("/ref"); assertEquals("Ref", refProperty.get("title").asText()); + var customPropertiesSchemaRequiredFields = customPropertiesSchema.withArray("/required"); + assertFalse(customPropertiesSchemaRequiredFields.isEmpty()); + assertEquals(1, customPropertiesSchemaRequiredFields.size(), "Size should be 1"); } @Test @@ -180,21 +183,26 @@ public void testDataFormatEnumParameter() throws Exception { @Test public void testGetLanguageCatalog() throws Exception { assertFalse(languageCatalog.has("file")); - var customModel = languageCatalog + var languageModel = languageCatalog .withObject("/language") .withObject("/model"); - assertEquals("model", customModel.get("kind").asText()); - assertEquals("Language", customModel.get("title").asText()); - var customProperties = languageCatalog + assertEquals("model", languageModel.get("kind").asText()); + assertEquals("Language", languageModel.get("title").asText()); + var languageProperties = languageCatalog .withObject("/language") .withObject("/properties"); - assertEquals("Language", customProperties.withObject("/language").get("displayName").asText()); - var customPropertiesSchema = languageCatalog + assertEquals("Language", languageProperties.withObject("/language").get("displayName").asText()); + var languagePropertiesSchema = languageCatalog .withObject("/language") .withObject("/propertiesSchema"); - assertEquals("Language", customPropertiesSchema.get("title").asText()); - var languageProperty = customPropertiesSchema.withObject("/properties").withObject("/language"); + assertEquals("Language", languagePropertiesSchema.get("title").asText()); + var languageProperty = languagePropertiesSchema.withObject("/properties").withObject("/language"); assertEquals("Language", languageProperty.get("title").asText()); + var languagePropertiesSchemaRequiredFields = languagePropertiesSchema.withArray("/required"); + assertFalse(languagePropertiesSchemaRequiredFields.isEmpty()); + assertEquals(2, languagePropertiesSchemaRequiredFields.size(), "Size should be 2"); + assertEquals("expression", languagePropertiesSchemaRequiredFields.get(0).asText()); + assertEquals("language", languagePropertiesSchemaRequiredFields.get(1).asText()); } @Test @@ -294,15 +302,25 @@ public void testGetLoadBalancerCatalog() throws Exception { var failoverModel = loadBalancerCatalog.withObject("/failoverLoadBalancer/model"); assertEquals("failoverLoadBalancer", failoverModel.get("name").asText()); var failoverSchema = loadBalancerCatalog.withObject("/failoverLoadBalancer/propertiesSchema"); + var failoverSchemaRequiredFields = failoverSchema.withArray("/required"); + assertTrue(failoverSchemaRequiredFields.isEmpty()); var maximumFailoverAttempts = failoverSchema.withObject("/properties/maximumFailoverAttempts"); assertEquals("string", maximumFailoverAttempts.get("type").asText()); assertEquals("-1", maximumFailoverAttempts.get("default").asText()); + var roundRobinSchema = loadBalancerCatalog.withObject("/roundRobinLoadBalancer/propertiesSchema"); + var roundRobinSchemaRequiredFields = roundRobinSchema.withArray("/required"); + assertTrue(roundRobinSchemaRequiredFields.isEmpty()); var roundRobinId = roundRobinSchema.withObject("/properties/id"); assertEquals("string", roundRobinId.get("type").asText()); + var customModel = loadBalancerCatalog.withObject("/customLoadBalancer/model"); assertEquals("Custom Load Balancer", customModel.get("title").asText()); var customSchema = loadBalancerCatalog.withObject("/customLoadBalancer/propertiesSchema"); + var customSchemaRequiredFields = customSchema.withArray("/required"); + assertFalse(customSchemaRequiredFields.isEmpty()); + assertEquals(1, customSchemaRequiredFields.size(), "Size should be 1"); + assertEquals("ref", customSchemaRequiredFields.get(0).asText()); assertEquals("Custom Load Balancer", customSchema.get("title").asText()); var customRef = customSchema.withObject("/properties/ref"); assertEquals("Ref", customRef.get("title").asText());