From 3563e7b752e4d3370e7e06666fcc04499724f77e Mon Sep 17 00:00:00 2001 From: Dilhasha Date: Thu, 21 Dec 2023 09:38:58 +0530 Subject: [PATCH] Add name for readonly records during config schema generation --- .../internal/configschema/TypeConverter.java | 20 ++++++++++++------ .../ComplexTypeConfigs2/expected-schema.json | 21 ++++++++++++++++++- .../ComplexTypeConfigs2/main.bal | 6 ++++++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/configschema/TypeConverter.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/configschema/TypeConverter.java index e4286cdddc38..ad128e640726 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/configschema/TypeConverter.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/internal/configschema/TypeConverter.java @@ -20,6 +20,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; import org.wso2.ballerinalang.compiler.semantics.analyzer.Types; +import org.wso2.ballerinalang.compiler.semantics.model.symbols.BTypeSymbol; import org.wso2.ballerinalang.compiler.semantics.model.symbols.Symbols; import org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType; import org.wso2.ballerinalang.compiler.semantics.model.types.BField; @@ -33,6 +34,7 @@ import org.wso2.ballerinalang.compiler.semantics.model.types.BUnionType; import org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral; import org.wso2.ballerinalang.compiler.tree.expressions.BLangNumericLiteral; +import org.wso2.ballerinalang.compiler.util.Names; import org.wso2.ballerinalang.compiler.util.TypeTags; import java.util.HashMap; @@ -181,14 +183,20 @@ private JsonObject generateRecordType(BRecordType effectiveType, BIntersectionTy if (!requiredFields.isEmpty()) { typeNode.add("required", requiredFields); } - // Get record type and set the type name as a property - for (BType bType : intersectionType.getConstituentTypes()) { - // Does not consider anonymous records - if (bType.tag == TypeTags.TYPEREFDESC) { - typeNode.addProperty("name", bType.toString().trim()); + BTypeSymbol intersectionSymbol = intersectionType.tsymbol; + // The tsymbol name is implicitly empty + if (intersectionSymbol.name != Names.EMPTY) { + typeNode.addProperty("name", intersectionSymbol.toString().trim()); + } else { + // Get record type and set the type name as a property + for (BType bType : intersectionType.getConstituentTypes()) { + // Does not consider anonymous records + if (bType.tag == TypeTags.TYPEREFDESC) { + // Revisit with https://github.com/ballerina-platform/ballerina-lang/issues/24078 + typeNode.addProperty("name", bType.toString().trim()); + } } } - return typeNode; } diff --git a/misc/compiler-plugins/modules/configurable-schema-generator/src/test/resources/DefaultModuleProjects/ComplexTypeConfigs2/expected-schema.json b/misc/compiler-plugins/modules/configurable-schema-generator/src/test/resources/DefaultModuleProjects/ComplexTypeConfigs2/expected-schema.json index 4ecaf0f1979d..38666331f1d7 100644 --- a/misc/compiler-plugins/modules/configurable-schema-generator/src/test/resources/DefaultModuleProjects/ComplexTypeConfigs2/expected-schema.json +++ b/misc/compiler-plugins/modules/configurable-schema-generator/src/test/resources/DefaultModuleProjects/ComplexTypeConfigs2/expected-schema.json @@ -140,12 +140,31 @@ } }, "description": "" + }, + "employeeReadOnly": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "salary": { + "type": "integer" + } + }, + "additionalProperties": false, + "required": [ + "name", + "salary" + ], + "name": "dilhashanazeer/complexconfigs2:0.1.0:EmployeeReadOnly", + "description": "" } }, "additionalProperties": false, "required": [ "employeeArray", - "empMapArray" + "empMapArray", + "employeeReadOnly" ] } }, diff --git a/misc/compiler-plugins/modules/configurable-schema-generator/src/test/resources/DefaultModuleProjects/ComplexTypeConfigs2/main.bal b/misc/compiler-plugins/modules/configurable-schema-generator/src/test/resources/DefaultModuleProjects/ComplexTypeConfigs2/main.bal index b853b3a28d88..13cc853ef4f8 100644 --- a/misc/compiler-plugins/modules/configurable-schema-generator/src/test/resources/DefaultModuleProjects/ComplexTypeConfigs2/main.bal +++ b/misc/compiler-plugins/modules/configurable-schema-generator/src/test/resources/DefaultModuleProjects/ComplexTypeConfigs2/main.bal @@ -10,6 +10,11 @@ type Employee record { int salary; }; +type EmployeeReadOnly readonly & record { + string name; + int salary; +}; + configurable table key(name) t = table [ { name: "John", salary: 100 }, { name: "Jane", salary: 200 } @@ -22,6 +27,7 @@ configurable map[] myArray = [{"val1" : 22}, {"val2" : 32}]; configurable Employee[] employeeArray = ?; configurable map employeeMap = {"emp1": {name: "user1", salary: 1200}}; configurable map[] empMapArray = ?; +configurable EmployeeReadOnly employeeReadOnly = ?; public function main() { }