From 100e4cbb2df139481acb526cf256b81927bc45a8 Mon Sep 17 00:00:00 2001 From: Felix Schnabel Date: Fri, 11 Oct 2024 01:10:48 +0200 Subject: [PATCH] Implement closed record with json rest fields for json-to-record-converter --- .../converters/JsonToRecordConverter.java | 113 ++++----- .../converters/util/ConverterUtils.java | 6 +- .../converters/util/SchemaGenerator.java | 2 +- .../JsonToRecordConverterTests.java | 214 +++++++++--------- .../test/resources/ballerina/basic_schema.bal | 5 +- .../resources/ballerina/closed_record.bal | 8 +- .../test/resources/ballerina/from_crlf.bal | 25 +- .../resources/ballerina/nested_object.bal | 15 +- .../ballerina/nested_object_closed_desc.bal | 8 +- .../ballerina/nested_object_type_desc.bal | 15 +- .../resources/ballerina/nested_schema.bal | 10 +- .../test/resources/ballerina/null_object.bal | 5 +- .../src/test/resources/ballerina/sample_1.bal | 15 +- .../test/resources/ballerina/sample_10.bal | 25 +- .../ballerina/sample_10_type_desc.bal | 25 +- .../ballerina/sample_11_type_desc.bal | 14 +- .../src/test/resources/ballerina/sample_2.bal | 15 +- .../src/test/resources/ballerina/sample_3.bal | 10 +- .../ballerina/sample_3_type_desc.bal | 11 +- .../src/test/resources/ballerina/sample_4.bal | 10 +- .../src/test/resources/ballerina/sample_5.bal | 5 +- .../src/test/resources/ballerina/sample_6.bal | 30 ++- .../ballerina/sample_6_type_desc.bal | 30 ++- .../ballerina/sample_7_type_desc.bal | 17 +- .../src/test/resources/ballerina/sample_8.bal | 105 +++++---- .../ballerina/sample_8_type_desc.bal | 161 +++++++------ .../src/test/resources/ballerina/sample_9.bal | 100 ++++---- .../ballerina/sample_9_type_desc.bal | 136 ++++++----- 28 files changed, 642 insertions(+), 493 deletions(-) diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/main/java/io/ballerina/converters/JsonToRecordConverter.java b/misc/ls-extensions/modules/json-to-record-converter/src/main/java/io/ballerina/converters/JsonToRecordConverter.java index a304dbeeb263..96aa74db6517 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/main/java/io/ballerina/converters/JsonToRecordConverter.java +++ b/misc/ls-extensions/modules/json-to-record-converter/src/main/java/io/ballerina/converters/JsonToRecordConverter.java @@ -32,6 +32,7 @@ import io.ballerina.compiler.syntax.tree.NodeList; import io.ballerina.compiler.syntax.tree.NonTerminalNode; import io.ballerina.compiler.syntax.tree.RecordFieldNode; +import io.ballerina.compiler.syntax.tree.RecordRestDescriptorNode; import io.ballerina.compiler.syntax.tree.RecordTypeDescriptorNode; import io.ballerina.compiler.syntax.tree.SyntaxKind; import io.ballerina.compiler.syntax.tree.Token; @@ -61,6 +62,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import static io.ballerina.compiler.syntax.tree.NodeFactory.createBuiltinSimpleNameReferenceNode; import static io.ballerina.converters.util.ConverterUtils.convertOpenAPITypeToBallerina; @@ -96,8 +98,8 @@ private JsonToRecordConverter() { * @throws FormatterException In case of invalid syntax */ @Deprecated - public static JsonToRecordResponse convert(String jsonString, String recordName, boolean isRecordTypeDesc, - boolean isClosed) throws IOException, + public static JsonToRecordResponse convert( + String jsonString, String recordName, boolean isRecordTypeDesc, boolean isClosed) throws IOException, JsonToRecordConverterException, FormatterException { return convert(jsonString, recordName, isRecordTypeDesc, isClosed, false); } @@ -116,12 +118,12 @@ public static JsonToRecordResponse convert(String jsonString, String recordName, * @throws FormatterException In case of invalid syntax */ public static JsonToRecordResponse convert(String jsonString, String recordName, boolean isRecordTypeDesc, - boolean isClosed, boolean forceFormatRecordFields) throws IOException, + boolean isClosed, boolean forceFormatRecordFields) throws IOException, JsonToRecordConverterException, FormatterException { - String name = ((recordName != null) && !recordName.isEmpty()) ? recordName : "NewRecord"; + String name = recordName != null && !recordName.isEmpty() ? recordName : "NewRecord"; ObjectMapper objectMapper = new ObjectMapper(); - OpenAPI model; JsonNode inputJson = objectMapper.readTree(jsonString); + OpenAPI model; if (inputJson.has("$schema")) { model = parseJSONSchema(jsonString, name); } else { @@ -144,8 +146,7 @@ public static JsonToRecordResponse convert(String jsonString, String recordName, .iterator().next().getValue(); Token semicolon = AbstractNodeFactory.createToken(SyntaxKind.SEMICOLON_TOKEN); Token typeKeyWord = AbstractNodeFactory.createToken(SyntaxKind.TYPE_KEYWORD); - IdentifierToken typeName = AbstractNodeFactory.createIdentifierToken( - escapeIdentifier(name)); + IdentifierToken typeName = AbstractNodeFactory.createIdentifierToken(escapeIdentifier(name)); TypeDefinitionNode typeDefinitionNode = NodeFactory.createTypeDefinitionNode(null, null, typeKeyWord, typeName, typeDescriptorNode, semicolon); NodeList moduleMembers = AbstractNodeFactory. @@ -156,8 +157,7 @@ public static JsonToRecordResponse convert(String jsonString, String recordName, } else { // Sets generated type definition code block NodeList moduleMembers = AbstractNodeFactory.createNodeList( - new ArrayList<>( - (Collection) (Collection) typeDefinitionNodes.values())); + (Collection) (Collection) typeDefinitionNodes.values()); Token eofToken = AbstractNodeFactory.createIdentifierToken(""); ModulePartNode modulePartNode = NodeFactory.createModulePartNode(imports, moduleMembers, eofToken); response.setCodeBlock(Formatter.format(modulePartNode.syntaxTree(), formattingOptions).toSourceCode()); @@ -176,9 +176,9 @@ public static JsonToRecordResponse convert(String jsonString, String recordName, * @throws JsonToRecordConverterException In case of bad record fields */ @SuppressWarnings("rawtypes") - private static Map generateRecords(OpenAPI openApi, boolean isRecordTypeDescriptor, - boolean isClosedRecord) - throws JsonToRecordConverterException { + private static Map generateRecords( + OpenAPI openApi, boolean isRecordTypeDescriptor, boolean isClosedRecord + ) throws JsonToRecordConverterException { Map typeDefinitionNodes = new LinkedHashMap<>(); Components components = openApi.getComponents(); @@ -186,7 +186,12 @@ private static Map generateRecords(OpenAPI openApi, boo if (components.getSchemas() == null || openApi.getComponents() == null) { return new LinkedHashMap<>(typeDefinitionNodes); } - + RecordRestDescriptorNode restDescriptorNode = isClosedRecord ? null : + NodeFactory.createRecordRestDescriptorNode( + NodeFactory.createBuiltinSimpleNameReferenceNode(SyntaxKind.JSON_KEYWORD, + AbstractNodeFactory.createToken(SyntaxKind.JSON_KEYWORD)), + AbstractNodeFactory.createToken(SyntaxKind.ELLIPSIS_TOKEN), + AbstractNodeFactory.createToken(SyntaxKind.SEMICOLON_TOKEN)); Map schemas = components.getSchemas(); for (Map.Entry schema: schemas.entrySet()) { List required = schema.getValue().getRequired(); @@ -198,9 +203,7 @@ private static Map generateRecords(OpenAPI openApi, boo Token recordKeyWord = AbstractNodeFactory.createToken(SyntaxKind.RECORD_KEYWORD); - Token bodyStartDelimiter = isClosedRecord ? - AbstractNodeFactory.createToken(SyntaxKind.OPEN_BRACE_PIPE_TOKEN) : - AbstractNodeFactory.createToken(SyntaxKind.OPEN_BRACE_TOKEN); + Token bodyStartDelimiter = AbstractNodeFactory.createToken(SyntaxKind.OPEN_BRACE_PIPE_TOKEN); List recordFieldList = new ArrayList<>(); Schema schemaValue = schema.getValue(); @@ -210,16 +213,14 @@ private static Map generateRecords(OpenAPI openApi, boo if (fields != null) { for (Map.Entry field : fields.entrySet()) { addRecordFields(required, recordFieldList, field, typeDefinitionNodes, - isRecordTypeDescriptor); + isRecordTypeDescriptor, restDescriptorNode); } } NodeList fieldNodes = AbstractNodeFactory.createNodeList(recordFieldList); - Token bodyEndDelimiter = isClosedRecord ? - AbstractNodeFactory.createToken(SyntaxKind.CLOSE_BRACE_PIPE_TOKEN) : - AbstractNodeFactory.createToken(SyntaxKind.CLOSE_BRACE_TOKEN); + Token bodyEndDelimiter = AbstractNodeFactory.createToken(SyntaxKind.CLOSE_BRACE_PIPE_TOKEN); RecordTypeDescriptorNode recordTypeDescriptorNode = NodeFactory.createRecordTypeDescriptorNode(recordKeyWord, bodyStartDelimiter, - fieldNodes, null, bodyEndDelimiter); + fieldNodes, restDescriptorNode, bodyEndDelimiter); String key = schema.getKey().trim(); if (isRecordTypeDescriptor) { typeDefinitionNodes.put(key, recordTypeDescriptorNode); @@ -240,10 +241,9 @@ private static Map generateRecords(OpenAPI openApi, boo TypeDescriptorNode fieldTypeName; if (arraySchema.getItems() != null) { fieldTypeName = extractOpenApiSchema(arraySchema.getItems(), schema.getKey(), - typeDefinitionNodes, isRecordTypeDescriptor); + typeDefinitionNodes, isRecordTypeDescriptor, restDescriptorNode); } else { - Token type = - AbstractNodeFactory.createToken(SyntaxKind.STRING_KEYWORD); + Token type = AbstractNodeFactory.createToken(SyntaxKind.STRING_KEYWORD); fieldTypeName = NodeFactory.createBuiltinSimpleNameReferenceNode(null, type); } @@ -254,10 +254,10 @@ private static Map generateRecords(OpenAPI openApi, boo null, arrayField, fieldName, null, semicolonToken); NodeList fieldNodes = AbstractNodeFactory.createNodeList(recordFieldNode); - Token bodyEndDelimiter = AbstractNodeFactory.createToken(SyntaxKind.CLOSE_BRACE_TOKEN); + Token bodyEndDelimiter = AbstractNodeFactory.createToken(SyntaxKind.CLOSE_BRACE_PIPE_TOKEN); RecordTypeDescriptorNode recordTypeDescriptorNode = NodeFactory.createRecordTypeDescriptorNode(recordKeyWord, bodyStartDelimiter, - fieldNodes, null, bodyEndDelimiter); + fieldNodes, restDescriptorNode, bodyEndDelimiter); if (isRecordTypeDescriptor) { typeDefinitionNodes.put(schema.getKey().trim() + "List", recordTypeDescriptorNode); } else { @@ -288,11 +288,12 @@ private static Map generateRecords(OpenAPI openApi, boo private static void addRecordFields(List required, List recordFieldList, Map.Entry field, Map typeDefinitionNodes, - boolean isRecordTypeDescriptor) + boolean isRecordTypeDescriptor, + RecordRestDescriptorNode restDescriptorNode) throws JsonToRecordConverterException { TypeDescriptorNode fieldTypeName = extractOpenApiSchema(field.getValue(), field.getKey(), - typeDefinitionNodes, isRecordTypeDescriptor); + typeDefinitionNodes, isRecordTypeDescriptor, restDescriptorNode); IdentifierToken fieldName = AbstractNodeFactory.createIdentifierToken(escapeIdentifier(field.getKey().trim())); Token questionMarkToken = (required != null && required.contains(field.getKey().trim())) @@ -317,10 +318,10 @@ private static void addRecordFields(List required, List recordFiel * @throws JsonToRecordConverterException In case of invalid schema */ @SuppressWarnings("rawtypes") - private static TypeDescriptorNode extractOpenApiSchema(Schema schema, String name, - Map typeDefinitionNodes, - boolean isRecordTypeDescriptor) - throws JsonToRecordConverterException { + private static TypeDescriptorNode extractOpenApiSchema( + Schema schema, String name, Map typeDefinitionNodes, + boolean isRecordTypeDescriptor, RecordRestDescriptorNode restDescriptorNode + ) throws JsonToRecordConverterException { if (schema.getType() != null || schema.getProperties() != null) { String schemaType = schema.getType(); @@ -337,20 +338,19 @@ private static TypeDescriptorNode extractOpenApiSchema(Schema schema, String String type; Token typeName; TypeDescriptorNode memberTypeDesc; - if (arraySchema.getItems().getType() != null && arraySchema.getItems().getType().equals("object")) { + if (Objects.equals(arraySchema.getItems().getType(), "object")) { type = StringUtils.capitalize(name) + "Item"; typeName = AbstractNodeFactory.createIdentifierToken(type); if (isRecordTypeDescriptor) { memberTypeDesc = extractOpenApiSchema(arraySchema.getItems(), type, typeDefinitionNodes, - isRecordTypeDescriptor); + true, restDescriptorNode); } else { memberTypeDesc = createBuiltinSimpleNameReferenceNode(null, typeName); - extractOpenApiSchema(arraySchema.getItems(), type, typeDefinitionNodes, - isRecordTypeDescriptor); + extractOpenApiSchema(arraySchema.getItems(), type, typeDefinitionNodes, false, restDescriptorNode); } } else if (arraySchema.getItems() instanceof ArraySchema) { memberTypeDesc = extractOpenApiSchema(arraySchema.getItems(), name, typeDefinitionNodes, - isRecordTypeDescriptor); + isRecordTypeDescriptor, restDescriptorNode); } else { type = arraySchema.getItems().getType(); typeName = AbstractNodeFactory.createIdentifierToken(convertOpenAPITypeToBallerina(type)); @@ -366,16 +366,16 @@ private static TypeDescriptorNode extractOpenApiSchema(Schema schema, String IdentifierToken typeName = AbstractNodeFactory.createIdentifierToken( escapeIdentifier(StringUtils.capitalize(name))); Token recordKeyWord = AbstractNodeFactory.createToken(SyntaxKind.RECORD_KEYWORD); - Token bodyStartDelimiter = AbstractNodeFactory.createToken(SyntaxKind.OPEN_BRACE_TOKEN); - Token bodyEndDelimiter = AbstractNodeFactory.createToken(SyntaxKind.CLOSE_BRACE_TOKEN); + Token bodyStartDelimiter = AbstractNodeFactory.createToken(SyntaxKind.OPEN_BRACE_PIPE_TOKEN); + Token bodyEndDelimiter = AbstractNodeFactory.createToken(SyntaxKind.CLOSE_BRACE_PIPE_TOKEN); List recordFList = new ArrayList<>(); for (Map.Entry property: properties.entrySet()) { addRecordFields(required, recordFList, property, typeDefinitionNodes, - isRecordTypeDescriptor); + isRecordTypeDescriptor, restDescriptorNode); } NodeList fieldNodes = AbstractNodeFactory.createNodeList(recordFList); TypeDescriptorNode typeDescriptorNode = NodeFactory.createRecordTypeDescriptorNode(recordKeyWord, - bodyStartDelimiter, fieldNodes, null, bodyEndDelimiter); + bodyStartDelimiter, fieldNodes, restDescriptorNode, bodyEndDelimiter); if (isRecordTypeDescriptor) { return typeDescriptorNode; @@ -389,19 +389,19 @@ private static TypeDescriptorNode extractOpenApiSchema(Schema schema, String } } else { - Token typeName = AbstractNodeFactory.createToken(SyntaxKind.ANY_KEYWORD); + Token typeName = AbstractNodeFactory.createToken(SyntaxKind.JSON_KEYWORD); return createBuiltinSimpleNameReferenceNode(null, typeName); } } else if (schema.get$ref() != null) { Token typeName = AbstractNodeFactory.createIdentifierToken(extractReferenceType(schema.get$ref())); return createBuiltinSimpleNameReferenceNode(null, typeName); } else { - //This contains a fallback to Ballerina common type `any` if the OpenApi specification type is not defined + //This contains a fallback to Ballerina common type `json` if the OpenApi specification type is not defined // or not compatible with any of the current Ballerina types. - Token typeName = AbstractNodeFactory.createToken(SyntaxKind.ANY_KEYWORD); + Token typeName = AbstractNodeFactory.createToken(SyntaxKind.JSON_KEYWORD); return createBuiltinSimpleNameReferenceNode(null, typeName); } - Token typeName = AbstractNodeFactory.createToken(SyntaxKind.ANY_KEYWORD); + Token typeName = AbstractNodeFactory.createToken(SyntaxKind.JSON_KEYWORD); return createBuiltinSimpleNameReferenceNode(null, typeName); } @@ -433,17 +433,18 @@ private static ArrayTypeDescriptorNode createArrayTypeDesc(TypeDescriptorNode me */ private static OpenAPI parseJSONSchema(String schemaString, String recordName) throws JsonToRecordConverterException, IOException { - final String prefix = "{\n" + - " \"openapi\" : \"3.0.1\",\n" + - " \"info\" : {\n" + - " \"title\" : \" payloadV\",\n" + - " \"version\" : \"1.0.0\"\n" + - " },\n" + - " \"servers\" : [],\n" + - " \"paths\" : {},\n" + - " \"components\" : {\n" + - " \"schemas\" : {\n" + - " \"" + recordName + "\" : "; + final String prefix = """ + { + "openapi" : "3.0.1", + "info" : { + "title" : " payloadV", + "version" : "1.0.0" + }, + "servers" : [], + "paths" : {}, + "components" : { + "schemas" : { + "%s" :\s""".formatted(recordName); final String suffix = """ diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/main/java/io/ballerina/converters/util/ConverterUtils.java b/misc/ls-extensions/modules/json-to-record-converter/src/main/java/io/ballerina/converters/util/ConverterUtils.java index d0e193dd2dee..19cfe5d8e2ad 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/main/java/io/ballerina/converters/util/ConverterUtils.java +++ b/misc/ls-extensions/modules/json-to-record-converter/src/main/java/io/ballerina/converters/util/ConverterUtils.java @@ -39,9 +39,9 @@ private ConverterUtils() { */ public static String convertOpenAPITypeToBallerina(String type) { - //In the case where type is not specified return anydata by default + //In the case where type is not specified return json by default if (type == null || type.isEmpty()) { - return "anydata"; + return "json"; } return switch (type) { @@ -54,7 +54,7 @@ public static String convertOpenAPITypeToBallerina(String type) { Constants.NUMBER, Constants.DOUBLE -> "decimal"; case Constants.FLOAT -> "float"; - default -> "anydata"; + default -> "json"; }; } diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/main/java/io/ballerina/converters/util/SchemaGenerator.java b/misc/ls-extensions/modules/json-to-record-converter/src/main/java/io/ballerina/converters/util/SchemaGenerator.java index 96155fd3a0a4..518ee68a3090 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/main/java/io/ballerina/converters/util/SchemaGenerator.java +++ b/misc/ls-extensions/modules/json-to-record-converter/src/main/java/io/ballerina/converters/util/SchemaGenerator.java @@ -82,7 +82,7 @@ public static Map generate(JsonNode json) throws JsonToRecordCon } if (json.getNodeType() == JsonNodeType.NULL) { - schema.put("type", "anydata"); + schema.put("type", "json"); return schema; } diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/java/io/ballerina/converters/JsonToRecordConverterTests.java b/misc/ls-extensions/modules/json-to-record-converter/src/test/java/io/ballerina/converters/JsonToRecordConverterTests.java index dc60ecfea4e8..fdeb35fdddb6 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/java/io/ballerina/converters/JsonToRecordConverterTests.java +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/java/io/ballerina/converters/JsonToRecordConverterTests.java @@ -24,12 +24,12 @@ import org.ballerinalang.langserver.util.TestUtil; import org.eclipse.lsp4j.jsonrpc.Endpoint; import org.testng.Assert; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; @@ -42,83 +42,94 @@ public class JsonToRecordConverterTests { private static final Path RES_DIR = Path.of("src/test/resources/").toAbsolutePath(); private static final String JsonToRecordService = "jsonToRecord/convert"; - private final Path basicSchemaJson = RES_DIR.resolve("json/basic_schema.json"); - private final Path basicSchemaBal = RES_DIR.resolve("ballerina/basic_schema.bal"); + private static final Path basicSchemaJson = RES_DIR.resolve("json/basic_schema.json"); + private static final Path basicSchemaBal = RES_DIR.resolve("ballerina/basic_schema.bal"); - private final Path invalidSchemaJson = RES_DIR.resolve("json/invalid_json_schema.json"); + private static final Path invalidSchemaJson = RES_DIR.resolve("json/invalid_json_schema.json"); - private final Path invalidJson = RES_DIR.resolve("json/invalid_json.json"); + private static final Path invalidJson = RES_DIR.resolve("json/invalid_json.json"); - private final Path nullJson = RES_DIR.resolve("json/null_json.json"); + private static final Path nullJson = RES_DIR.resolve("json/null_json.json"); - private final Path basicObjectJson = RES_DIR.resolve("json/basic_object.json"); - private final Path basicObjectBal = RES_DIR.resolve("ballerina/basic_object.bal"); + private static final Path basicObjectJson = RES_DIR.resolve("json/basic_object.json"); + private static final Path basicObjectBal = RES_DIR.resolve("ballerina/basic_object.bal"); - private final Path nestedSchemaJson = RES_DIR.resolve("json/nested_schema.json"); - private final Path nestedSchemaBal = RES_DIR.resolve("ballerina/nested_schema.bal"); + private static final Path nestedSchemaJson = RES_DIR.resolve("json/nested_schema.json"); + private static final Path nestedSchemaBal = RES_DIR.resolve("ballerina/nested_schema.bal"); - private final Path nestedObjectJson = RES_DIR.resolve("json/nested_object.json"); - private final Path nestedObjectBal = RES_DIR.resolve("ballerina/nested_object.bal"); + private static final Path nestedObjectJson = RES_DIR.resolve("json/nested_object.json"); + private static final Path nestedObjectBal = RES_DIR.resolve("ballerina/nested_object.bal"); - private final Path nullObjectJson = RES_DIR.resolve("json/null_object.json"); - private final Path nullObjectBal = RES_DIR.resolve("ballerina/null_object.bal"); - private final Path nullObjectDirectConversionBal = RES_DIR.resolve("ballerina/null_object_direct_conversion.bal"); + private static final Path nullObjectJson = RES_DIR.resolve("json/null_object.json"); + private static final Path nullObjectBal = RES_DIR.resolve("ballerina/null_object.bal"); + private static final Path nullObjectDirectConversionBal = RES_DIR.resolve("ballerina/null_object_direct_conversion.bal"); - private final Path sample1Json = RES_DIR.resolve("json/sample_1.json"); - private final Path sample1Bal = RES_DIR.resolve("ballerina/sample_1.bal"); + private static final Path sample1Json = RES_DIR.resolve("json/sample_1.json"); + private static final Path sample1Bal = RES_DIR.resolve("ballerina/sample_1.bal"); - private final Path sample2Json = RES_DIR.resolve("json/sample_2.json"); - private final Path sample2Bal = RES_DIR.resolve("ballerina/sample_2.bal"); + private static final Path sample2Json = RES_DIR.resolve("json/sample_2.json"); + private static final Path sample2Bal = RES_DIR.resolve("ballerina/sample_2.bal"); - private final Path sample3Json = RES_DIR.resolve("json/sample_3.json"); - private final Path sample3Bal = RES_DIR.resolve("ballerina/sample_3.bal"); - private final Path sample3TypeDescBal = RES_DIR.resolve("ballerina/sample_3_type_desc.bal"); + private static final Path sample3Json = RES_DIR.resolve("json/sample_3.json"); + private static final Path sample3Bal = RES_DIR.resolve("ballerina/sample_3.bal"); + private static final Path sample3TypeDescBal = RES_DIR.resolve("ballerina/sample_3_type_desc.bal"); - private final Path sample4Json = RES_DIR.resolve("json/sample_4.json"); - private final Path sample4Bal = RES_DIR.resolve("ballerina/sample_4.bal"); + private static final Path sample4Json = RES_DIR.resolve("json/sample_4.json"); + private static final Path sample4Bal = RES_DIR.resolve("ballerina/sample_4.bal"); - private final Path sample5Json = RES_DIR.resolve("json/sample_5.json"); - private final Path sample5Bal = RES_DIR.resolve("ballerina/sample_5.bal"); + private static final Path sample5Json = RES_DIR.resolve("json/sample_5.json"); + private static final Path sample5Bal = RES_DIR.resolve("ballerina/sample_5.bal"); - private final Path sample6Json = RES_DIR.resolve("json/sample_6.json"); - private final Path sample6Bal = RES_DIR.resolve("ballerina/sample_6.bal"); - private final Path sample6TypeDescBal = RES_DIR.resolve("ballerina/sample_6_type_desc.bal"); + private static final Path sample6Json = RES_DIR.resolve("json/sample_6.json"); + private static final Path sample6Bal = RES_DIR.resolve("ballerina/sample_6.bal"); + private static final Path sample6TypeDescBal = RES_DIR.resolve("ballerina/sample_6_type_desc.bal"); - private final Path sample7Json = RES_DIR.resolve("json/sample_7.json"); - private final Path sample7TypeDescBal = RES_DIR.resolve("ballerina/sample_7_type_desc.bal"); + private static final Path sample7Json = RES_DIR.resolve("json/sample_7.json"); + private static final Path sample7TypeDescBal = RES_DIR.resolve("ballerina/sample_7_type_desc.bal"); - private final Path sample8Json = RES_DIR.resolve("json/sample_8.json"); - private final Path sample8Bal = RES_DIR.resolve("ballerina/sample_8.bal"); - private final Path sample8TypeDescBal = RES_DIR.resolve("ballerina/sample_8_type_desc.bal"); + private static final Path sample8Json = RES_DIR.resolve("json/sample_8.json"); + private static final Path sample8Bal = RES_DIR.resolve("ballerina/sample_8.bal"); + private static final Path sample8TypeDescBal = RES_DIR.resolve("ballerina/sample_8_type_desc.bal"); - private final Path sample9Json = RES_DIR.resolve("json/sample_9.json"); - private final Path sample9Bal = RES_DIR.resolve("ballerina/sample_9.bal"); - private final Path sample9TypeDescBal = RES_DIR.resolve("ballerina/sample_9_type_desc.bal"); + private static final Path sample9Json = RES_DIR.resolve("json/sample_9.json"); + private static final Path sample9Bal = RES_DIR.resolve("ballerina/sample_9.bal"); + private static final Path sample9TypeDescBal = RES_DIR.resolve("ballerina/sample_9_type_desc.bal"); - private final Path sample10Json = RES_DIR.resolve("json/sample_10.json"); - private final Path sample10Bal = RES_DIR.resolve("ballerina/sample_10.bal"); - private final Path sample10TypeDescBal = RES_DIR.resolve("ballerina/sample_10_type_desc.bal"); - private final Path sample10WithoutConflictBal = RES_DIR.resolve("ballerina/sample_10_without_conflict.bal"); + private static final Path sample10Json = RES_DIR.resolve("json/sample_10.json"); + private static final Path sample10Bal = RES_DIR.resolve("ballerina/sample_10.bal"); + private static final Path sample10TypeDescBal = RES_DIR.resolve("ballerina/sample_10_type_desc.bal"); + private static final Path sample10WithoutConflictBal = RES_DIR.resolve("ballerina/sample_10_without_conflict.bal"); - private final Path sample11Json = RES_DIR.resolve("json/sample_11.json"); - private final Path sample11TypeDescBal = RES_DIR.resolve("ballerina/sample_11_type_desc.bal"); + private static final Path sample11Json = RES_DIR.resolve("json/sample_11.json"); + private static final Path sample11TypeDescBal = RES_DIR.resolve("ballerina/sample_11_type_desc.bal"); - private final Path crlfJson = RES_DIR.resolve("json/crlf.json"); - private final Path crlfBal = RES_DIR.resolve("ballerina/from_crlf.bal"); + private static final Path crlfJson = RES_DIR.resolve("json/crlf.json"); + private static final Path crlfBal = RES_DIR.resolve("ballerina/from_crlf.bal"); - private final Path nestedObjectTypeDescBal = RES_DIR.resolve("ballerina/nested_object_type_desc.bal"); + private static final Path nestedObjectTypeDescBal = RES_DIR.resolve("ballerina/nested_object_type_desc.bal"); - private final Path nestedObjectClosedDescBal = RES_DIR.resolve("ballerina/nested_object_closed_desc.bal"); + private static final Path nestedObjectClosedDescBal = RES_DIR.resolve("ballerina/nested_object_closed_desc.bal"); - private final Path closedRecordBal = RES_DIR.resolve("ballerina/closed_record.bal"); + private static final Path closedRecordBal = RES_DIR.resolve("ballerina/closed_record.bal"); - private final Path emptyArrayJson = RES_DIR.resolve("json/empty_array.json"); + private static final Path emptyArrayJson = RES_DIR.resolve("json/empty_array.json"); + + @DataProvider + public static Object[][] samples() { + return new Object[][] { + new Path[]{sample3Json, sample3TypeDescBal}, + new Path[]{sample6Json, sample6TypeDescBal}, + new Path[]{sample8Json, sample8TypeDescBal}, + new Path[]{sample9Json, sample9TypeDescBal}, + new Path[]{sample10Json, sample10TypeDescBal} + }; + } @Test(description = "Test with basic json schema string") public void testBasicSchema() throws JsonToRecordConverterException, IOException, FormatterException { String jsonFileContent = Files.readString(basicSchemaJson); - String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", false, - false, false).getCodeBlock().replaceAll("\\s+", ""); + String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", false, false, false) + .getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(basicSchemaBal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -126,8 +137,8 @@ public void testBasicSchema() throws JsonToRecordConverterException, IOException @Test(description = "Test with basic json object") public void testBasicJson() throws JsonToRecordConverterException, IOException, FormatterException { String jsonFileContent = Files.readString(basicObjectJson); - String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", false, - false, false).getCodeBlock().replaceAll("\\s+", ""); + String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", false, false, false) + .getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(basicObjectBal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -135,8 +146,8 @@ public void testBasicJson() throws JsonToRecordConverterException, IOException, @Test(description = "Test get type record descriptor nested objects") public void testNestedSchema() throws JsonToRecordConverterException, IOException, FormatterException { String jsonFileContent = Files.readString(nestedSchemaJson); - String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", false, - false, false).getCodeBlock().replaceAll("\\s+", ""); + String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", false, false, false) + .getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(nestedSchemaBal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -145,8 +156,8 @@ public void testNestedSchema() throws JsonToRecordConverterException, IOExceptio public void testBasicObjectForRecordTypeDesc() throws JsonToRecordConverterException, IOException, FormatterException { String jsonFileContent = Files.readString(basicObjectJson); - String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", - true, false, false).getCodeBlock().replaceAll("\\s+", ""); + String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", true, false, false) + .getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(basicObjectBal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -155,8 +166,8 @@ public void testBasicObjectForRecordTypeDesc() throws public void testTypeDescCodeForNestedObjects() throws JsonToRecordConverterException, IOException, FormatterException { String jsonFileContent = Files.readString(nestedObjectJson); - String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", - true, false, false).getCodeBlock().replaceAll("\\s+", ""); + String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", true, false, false) + .getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(nestedObjectTypeDescBal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -165,8 +176,8 @@ public void testTypeDescCodeForNestedObjects() throws public void testClosedNestedSchemaForRecordTypeDesc() throws JsonToRecordConverterException, IOException, FormatterException { String jsonFileContent = Files.readString(nestedObjectJson); - String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", true, - true, false).getCodeBlock().replaceAll("\\s+", ""); + String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", true, true, false) + .getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(nestedObjectClosedDescBal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -174,8 +185,8 @@ public void testClosedNestedSchemaForRecordTypeDesc() throws @Test(description = "Test json with nested objects") public void testNestedJson() throws JsonToRecordConverterException, IOException, FormatterException { String jsonFileContent = Files.readString(nestedObjectJson); - String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", false, - false, false).getCodeBlock().replaceAll("\\s+", ""); + String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", false, false, false) + .getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(nestedObjectBal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -190,24 +201,24 @@ public void testRefTypeExtraction() throws JsonToRecordConverterException { @Test(description = "Test with CRLF formatted json file") public void testCRLFJson() throws JsonToRecordConverterException, IOException, FormatterException { String jsonFileContent = Files.readString(crlfJson); - String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", false, - false, false).getCodeBlock().replaceAll("\\s+", ""); + String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", false, false, false) + .getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(crlfBal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @Test(description = "Test with sample json objects") public void testSamples() throws JsonToRecordConverterException, IOException, FormatterException { - Map samples = new HashMap<>(); - samples.put(sample1Json, sample1Bal); - samples.put(sample2Json, sample2Bal); - samples.put(sample3Json, sample3Bal); - samples.put(sample4Json, sample4Bal); - samples.put(sample5Json, sample5Bal); - samples.put(sample6Json, sample6Bal); - samples.put(sample8Json, sample8Bal); - samples.put(sample9Json, sample9Bal); - samples.put(sample10Json, sample10Bal); + Map samples = Map.of( + sample1Json, sample1Bal, + sample2Json, sample2Bal, + sample3Json, sample3Bal, + sample4Json, sample4Bal, + sample5Json, sample5Bal, + sample6Json, sample6Bal, + sample8Json, sample8Bal, + sample9Json, sample9Bal, + sample10Json, sample10Bal); for (Map.Entry sample : samples.entrySet()) { String jsonFileContent = Files.readString(sample.getKey()); String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", @@ -218,28 +229,20 @@ public void testSamples() throws JsonToRecordConverterException, IOException, Fo } } - @Test(description = "Test with sample json objects for fields") - public void testFieldForSamples() throws JsonToRecordConverterException, IOException, FormatterException { - Map samples = new HashMap<>(); - samples.put(sample3Json, sample3TypeDescBal); - samples.put(sample6Json, sample6TypeDescBal); - samples.put(sample8Json, sample8TypeDescBal); - samples.put(sample9Json, sample9TypeDescBal); - samples.put(sample10Json, sample10TypeDescBal); - for (Map.Entry sample : samples.entrySet()) { - String jsonFileContent = Files.readString(sample.getKey()); - String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "Person", - true, false, false).getCodeBlock().replaceAll("\\s+", ""); - String expectedCodeBlock = Files.readString(sample.getValue()).replaceAll("\\s+", ""); - Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); - } + @Test(description = "Test with sample json objects for fields", dataProvider = "samples") + public void testFieldForSamples(Path json, Path bal) throws JsonToRecordConverterException, IOException, FormatterException { + String jsonFileContent = Files.readString(json); + String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "Person", true, false, false) + .getCodeBlock().replaceAll("\\s+", ""); + String expectedCodeBlock = Files.readString(bal).replaceAll("\\s+", ""); + Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @Test(description = "Test a sample json for a closed record") public void testClosedRecord() throws JsonToRecordConverterException, IOException, FormatterException { String jsonFileContent = Files.readString(sample7Json); - String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", - false, true, false).getCodeBlock().replaceAll("\\s+", ""); + String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", false, true, false) + .getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(closedRecordBal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -247,8 +250,8 @@ public void testClosedRecord() throws JsonToRecordConverterException, IOExceptio @Test(description = "Test with sample json for a closed record and objects for fields") public void testFieldForClosedRecord() throws JsonToRecordConverterException, IOException, FormatterException { String jsonFileContent = Files.readString(sample7Json); - String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", - true, false, false).getCodeBlock().replaceAll("\\s+", ""); + String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", true, false, false) + .getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample7TypeDescBal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } @@ -257,8 +260,7 @@ public void testFieldForClosedRecord() throws JsonToRecordConverterException, IO public void testNullObject() throws IOException, FormatterException { String jsonFileContent = Files.readString(nullObjectJson); try { - JsonToRecordConverter.convert(jsonFileContent, "", - true, false, false).getCodeBlock() + JsonToRecordConverter.convert(jsonFileContent, "", true, false, false).getCodeBlock() .replaceAll("\\s+", ""); Assert.assertTrue(true); } catch (JsonToRecordConverterException e) { @@ -314,8 +316,7 @@ public void testJSON2RecordServiceJSONSchema() throws IOException, ExecutionExce Endpoint serviceEndpoint = TestUtil.initializeLanguageSever(); String jsonString = Files.readString(basicSchemaJson); - JsonToRecordRequest request = new JsonToRecordRequest(jsonString, null, - false, false, false, null, false); + JsonToRecordRequest request = new JsonToRecordRequest(jsonString, null, false, false, false, null, false); CompletableFuture result = serviceEndpoint.request(JsonToRecordService, request); io.ballerina.jsonmapper.JsonToRecordResponse response = (io.ballerina.jsonmapper.JsonToRecordResponse) result.get(); @@ -329,8 +330,7 @@ public void testJSON2RecordServiceInvalidJSONSchema() throws IOException, Execut Endpoint serviceEndpoint = TestUtil.initializeLanguageSever(); String jsonString = Files.readString(invalidSchemaJson); - JsonToRecordRequest request = new JsonToRecordRequest(jsonString, null, - false, false, false, null, false); + JsonToRecordRequest request = new JsonToRecordRequest(jsonString, null, false, false, false, null, false); CompletableFuture result = serviceEndpoint.request(JsonToRecordService, request); io.ballerina.jsonmapper.JsonToRecordResponse response = (io.ballerina.jsonmapper.JsonToRecordResponse) result.get(); @@ -344,8 +344,7 @@ public void testJSON2RecordServiceInvalidJSONObject() throws IOException, Execut Endpoint serviceEndpoint = TestUtil.initializeLanguageSever(); String jsonString = Files.readString(invalidJson); - JsonToRecordRequest request = new JsonToRecordRequest(jsonString, null, - false, false, false, null, false); + JsonToRecordRequest request = new JsonToRecordRequest(jsonString, null, false, false, false, null, false); CompletableFuture result = serviceEndpoint.request(JsonToRecordService, request); io.ballerina.jsonmapper.JsonToRecordResponse response = (io.ballerina.jsonmapper.JsonToRecordResponse) result.get(); @@ -361,8 +360,7 @@ public void testJSON2RecordServiceNullJSON() throws IOException, ExecutionExcept Endpoint serviceEndpoint = TestUtil.initializeLanguageSever(); String jsonString = Files.readString(nullJson); - JsonToRecordRequest request = new JsonToRecordRequest(jsonString, null, - false, false, false, null, false); + JsonToRecordRequest request = new JsonToRecordRequest(jsonString, null, false, false, false, null, false); CompletableFuture result = serviceEndpoint.request(JsonToRecordService, request); io.ballerina.jsonmapper.JsonToRecordResponse response = (io.ballerina.jsonmapper.JsonToRecordResponse) result.get(); @@ -379,8 +377,8 @@ public void testJSON2RecordServiceConflictingRecNames() throws IOException, Exec Endpoint serviceEndpoint = TestUtil.initializeLanguageSever(); String jsonString = Files.readString(sample10Json); - JsonToRecordRequest request = new JsonToRecordRequest(jsonString, null, - false, false, false, sample10Bal.toUri().toString(), false); + JsonToRecordRequest request = new JsonToRecordRequest(jsonString, null, false, false, false, + sample10Bal.toUri().toString(), false); CompletableFuture result = serviceEndpoint.request(JsonToRecordService, request); io.ballerina.jsonmapper.JsonToRecordResponse response = (io.ballerina.jsonmapper.JsonToRecordResponse) result.get(); @@ -392,8 +390,8 @@ public void testJSON2RecordServiceConflictingRecNames() throws IOException, Exec @Test(description = "Test null and empty array field type extraction") public void testNullAndEmptyArray() throws JsonToRecordConverterException, IOException, FormatterException { String jsonFileContent = Files.readString(sample11Json); - String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", - true, false, false).getCodeBlock().replaceAll("\\s+", ""); + String generatedCodeBlock = JsonToRecordConverter.convert(jsonFileContent, "", true, false, false) + .getCodeBlock().replaceAll("\\s+", ""); String expectedCodeBlock = Files.readString(sample11TypeDescBal).replaceAll("\\s+", ""); Assert.assertEquals(generatedCodeBlock, expectedCodeBlock); } diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/basic_schema.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/basic_schema.bal index 5f1abb9c630e..a1d49db21f2c 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/basic_schema.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/basic_schema.bal @@ -1,7 +1,8 @@ -type NewRecord record { +type NewRecord record {| string name; int id; boolean bark?; string breed?; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/closed_record.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/closed_record.bal index aee9e36b39dc..1dae26c0af66 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/closed_record.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/closed_record.bal @@ -1,13 +1,13 @@ -type City record { +type City record {| string code; string name; -}; +|}; -type Address record { +type Address record {| string country; City city; string Lane; -}; +|}; type NewRecord record {| Address address; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/from_crlf.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/from_crlf.bal index 57765662bdbe..87d8cd3b9175 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/from_crlf.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/from_crlf.bal @@ -1,19 +1,21 @@ -type Image record { +type Image record {| int vOffset; string src; string name; string alignment; int hOffset; -}; + json...; +|}; -type Window record { +type Window record {| string name; int width; string title; int height; -}; + json...; +|}; -type Text record { +type Text record {| int vOffset; string data; int size; @@ -22,16 +24,19 @@ type Text record { string alignment; string onMouseUp; int hOffset; -}; + json...; +|}; -type Widget record { +type Widget record {| Image image; string debug; Window window; Text text; -}; + json...; +|}; -type NewRecord record { +type NewRecord record {| Widget widget; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_object.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_object.bal index 8119b40ba133..10406f450ae5 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_object.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_object.bal @@ -1,18 +1,21 @@ -type ColorsItem record { +type ColorsItem record {| string name; int id; -}; + json...; +|}; -type Dimensions record { +type Dimensions record {| int width; int height; -}; + json...; +|}; -type NewRecord record { +type NewRecord record {| string name; boolean checked; string id; ColorsItem[] colors; Dimensions dimensions; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_object_closed_desc.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_object_closed_desc.bal index ec92db59195a..1c2a0ac106f2 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_object_closed_desc.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_object_closed_desc.bal @@ -2,12 +2,12 @@ type NewRecord record {| string name; boolean checked; string id; - record { + record {| string name; int id; - }[] colors; - record { + |}[] colors; + record {| int width; int height; - } dimensions; + |} dimensions; |}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_object_type_desc.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_object_type_desc.bal index cb0ebac443bb..25b6262bc2ac 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_object_type_desc.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_object_type_desc.bal @@ -1,15 +1,18 @@ -type NewRecord record { +type NewRecord record {| string name; boolean checked; string id; - record { + record {| string name; int id; - }[] colors; + json...; + |}[] colors; - record { + record {| int width; int height; - } dimensions; -}; + json...; + |} dimensions; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_schema.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_schema.bal index 0381716c7b5a..8199d39752f6 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_schema.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/nested_schema.bal @@ -1,12 +1,14 @@ -type Dimensions record { +type Dimensions record {| int width; int height; -}; + json...; +|}; -type NewRecord record { +type NewRecord record {| boolean checked; Dimensions dimensions; int id; string name; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/null_object.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/null_object.bal index c3d94bbfb34c..dd3d1c90ad7d 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/null_object.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/null_object.bal @@ -1,3 +1,4 @@ -type NewRecord record { +type NewRecord record {| json id; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_1.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_1.bal index 4ece2a470664..c091f438811d 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_1.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_1.bal @@ -1,17 +1,20 @@ -type SportsItem record { +type SportsItem record {| string question; string answer; string[] options; -}; + json...; +|}; -type MathsItem record { +type MathsItem record {| string question; string answer; string[] options; -}; + json...; +|}; -type NewRecord record { +type NewRecord record {| SportsItem[] sports; MathsItem[] maths; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_10.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_10.bal index a41fd3ff46ad..b157d84c2205 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_10.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_10.bal @@ -1,26 +1,31 @@ -type BatterItem record { +type BatterItem record {| string id; string 'type; -}; + json...; +|}; -type Batters record { +type Batters record {| BatterItem[] batter; -}; + json...; +|}; -type ToppingItem record { +type ToppingItem record {| string id; string 'type; -}; + json...; +|}; -type NewRecord record { +type NewRecord record {| decimal ppu; Batters batters; string name; string id; string 'type; ToppingItem[] topping; -}; + json...; +|}; -type NewRecordList record { +type NewRecordList record {| NewRecord[] newrecordlist; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_10_type_desc.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_10_type_desc.bal index ddeeba04628a..b493dd511d66 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_10_type_desc.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_10_type_desc.bal @@ -1,22 +1,27 @@ -type Person record { +type Person record {| - record { + record {| decimal ppu; - record { - record { + record {| + record {| string id; string 'type; - }[] batter; - } batters; + json...; + |}[] batter; + json...; + |} batters; string name; string id; string 'type; - record { + record {| string id; string 'type; - }[] topping; - }[] personlist; -}; + json...; + |}[] topping; + json...; + |}[] personlist; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_11_type_desc.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_11_type_desc.bal index 34298cd08b48..4af6596321b2 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_11_type_desc.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_11_type_desc.bal @@ -1,9 +1,11 @@ -type NewRecord record { - record { - anydata[] donations; - any subscription; - } contributions; +type NewRecord record {| + record {| + json[] donations; + json subscription; + json...; + |} contributions; string school; string name; int age; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_2.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_2.bal index 739f65585442..9f932bde9de7 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_2.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_2.bal @@ -1,20 +1,23 @@ -type Address record { +type Address record {| string streetAddress; string city; string state; -}; + json...; +|}; -type PhoneNumbersItem record { +type PhoneNumbersItem record {| string number; string 'type; -}; + json...; +|}; -type NewRecord record { +type NewRecord record {| string firstName; string lastName; Address address; string gender; int age; PhoneNumbersItem[] phoneNumbers; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_3.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_3.bal index 7a897c17b726..4cec6f6448e8 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_3.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_3.bal @@ -1,8 +1,10 @@ -type NewRecord record { +type NewRecord record {| string color; string value; -}; + json...; +|}; -type NewRecordList record { +type NewRecordList record {| NewRecord[] newrecordlist; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_3_type_desc.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_3_type_desc.bal index 8bb3e8011825..c7f44e47de70 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_3_type_desc.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_3_type_desc.bal @@ -1,5 +1,6 @@ -type Person record { - record { - string color; string value; - }[] personlist; -}; +type Person record {| + record {| + string color; string value; json...; + |}[] personlist; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_4.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_4.bal index e783b832d515..c33618e062fa 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_4.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_4.bal @@ -1,12 +1,14 @@ -type PeopleItem record { +type PeopleItem record {| string firstName; string lastName; string number; string gender; int age; -}; + json...; +|}; -type NewRecord record { +type NewRecord record {| PeopleItem[] people; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_5.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_5.bal index 17d4ae2eb8a0..503b9beb0603 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_5.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_5.bal @@ -1,6 +1,7 @@ -type NewRecord record { +type NewRecord record {| string size; string color; string fruit; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_6.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_6.bal index 2f01a1ee3894..6742c7e7cd21 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_6.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_6.bal @@ -1,33 +1,38 @@ -type Author record { +type Author record {| string country; string period; string name; -}; + json...; +|}; -type BooksItem record { +type BooksItem record {| Author author; string name; -}; + json...; +|}; -type State record { +type State record {| string code; string name; -}; + json...; +|}; -type Address record { +type Address record {| string number; string city; string street; string neighborhood; State state; -}; + json...; +|}; -type SportsItem record { +type SportsItem record {| string position; string sport; -}; + json...; +|}; -type NewRecord record { +type NewRecord record {| BooksItem[] books; Address address; SportsItem[] sports; @@ -36,5 +41,6 @@ type NewRecord record { string name; int age; boolean honors; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_6_type_desc.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_6_type_desc.bal index 319e5dacc325..24733fee605f 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_6_type_desc.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_6_type_desc.bal @@ -1,32 +1,38 @@ -type Person record { - record { - record { +type Person record {| + record {| + record {| string country; string period; string name; - } author; + json...; + |} author; string name; - }[] books; + json...; + |}[] books; - record { + record {| string number; string city; string street; string neighborhood; - record { + record {| string code; string name; - } state; - } address; + json...; + |} state; + json...; + |} address; - record { + record {| string position; string sport; - }[] sports; + json...; + |}[] sports; string school; string year; string name; int age; boolean honors; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_7_type_desc.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_7_type_desc.bal index 967ac4734604..99317960688f 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_7_type_desc.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_7_type_desc.bal @@ -1,14 +1,15 @@ -type NewRecord record { - record { +type NewRecord record {| + record {| string country; - record { - string code; string name; - } city; string Lane; - - } address; + record {| + string code; string name; json...; + |} city; string Lane; + json...; + |} address; string school; string name; int age; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_8.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_8.bal index b17d02370b88..d83ed69b7c20 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_8.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_8.bal @@ -1,44 +1,52 @@ -type Coverage record { +type Coverage record {| string reference; -}; + json...; +|}; -type InsuranceItem record { +type InsuranceItem record {| Coverage coverage; int sequence; boolean focal; -}; + json...; +|}; -type IdentifierItem record { +type IdentifierItem record {| string system; string value; -}; + json...; +|}; -type UnitPrice record { +type UnitPrice record {| string currency; decimal value; -}; + json...; +|}; -type CodingItem record { +type CodingItem record {| string code; -}; + json...; +|}; -type ProductOrService record { +type ProductOrService record {| CodingItem[] coding; -}; + json...; +|}; -type Net record { +type Net record {| string currency; decimal value; -}; + json...; +|}; -type DetailItem record { +type DetailItem record {| UnitPrice unitPrice; int sequence; ProductOrService productOrService; Net net; -}; + json...; +|}; -type ItemItem record { +type ItemItem record {| UnitPrice unitPrice; int sequence; int[] careTeamSequence; @@ -46,56 +54,68 @@ type ItemItem record { string servicedDate; DetailItem[] detail; Net net; -}; + json...; +|}; -type DiagnosisCodeableConcept record { +type DiagnosisCodeableConcept record {| CodingItem[] coding; -}; + json...; +|}; -type DiagnosisItem record { +type DiagnosisItem record {| int sequence; DiagnosisCodeableConcept diagnosisCodeableConcept; -}; + json...; +|}; -type Type record { +type Type record {| CodingItem[] coding; -}; + json...; +|}; -type Priority record { +type Priority record {| CodingItem[] coding; -}; + json...; +|}; -type Payee record { +type Payee record {| Type 'type; -}; + json...; +|}; -type Provider record { +type Provider record {| string reference; -}; + json...; +|}; -type Prescription record { +type Prescription record {| string reference; -}; + json...; +|}; -type Patient record { +type Patient record {| string reference; -}; + json...; +|}; -type Insurer record { +type Insurer record {| string reference; -}; + json...; +|}; -type Text record { +type Text record {| string div; string status; -}; + json...; +|}; -type CareTeamItem record { +type CareTeamItem record {| int sequence; Provider provider; -}; + json...; +|}; -type NewRecord record { +type NewRecord record {| InsuranceItem[] insurance; IdentifierItem[] identifier; ItemItem[] item; @@ -114,4 +134,5 @@ type NewRecord record { CareTeamItem[] careTeam; string resourceType; string status; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_8_type_desc.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_8_type_desc.bal index 69018d4f913f..e3d3e0348fcc 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_8_type_desc.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_8_type_desc.bal @@ -1,124 +1,155 @@ -type Person record { - record { - record { +type Person record {| + record {| + record {| string reference; - } coverage; + json...; + |} coverage; int sequence; boolean focal; - }[] insurance; + json...; + |}[] insurance; - record { + record {| string system; string value; - }[] identifier; + json...; + |}[] identifier; - record { - record { + record {| + record {| string currency; decimal value; - } unitPrice; + json...; + |} unitPrice; int sequence; int[] careTeamSequence; - record { - record { + record {| + record {| string system; string code; - }[] coding; - } productOrService; + json...; + |}[] coding; + json...; + |} productOrService; string servicedDate; - record { - record { + record {| + record {| string currency; decimal value; - } unitPrice; + json...; + |} unitPrice; int sequence; - record { - record { + record {| + record {| string system; string code; - }[] coding; - } productOrService; + json...; + |}[] coding; + json...; + |} productOrService; - record { + record {| string currency; decimal value; - } net; - }[] detail; + json...; + |} net; + json...; + |}[] detail; - record { + record {| string currency; decimal value; - } net; - }[] item; + json...; + |} net; + json...; + |}[] item; string use; string created; - record { + record {| int sequence; - record { - record { + record {| + record {| string code; - }[] coding; - } diagnosisCodeableConcept; - }[] diagnosis; - - record { - record { + json...; + |}[] coding; + json...; + |} diagnosisCodeableConcept; + json...; + |}[] diagnosis; + + record {| + record {| string system; string code; - }[] coding; - } 'type; + json...; + |}[] coding; + json...; + |} 'type; - record { - record { + record {| + record {| string code; - }[] coding; - } priority; - - record { - record { - record { + json...; + |}[] coding; + json...; + |} priority; + + record {| + record {| + record {| string code; - }[] coding; - } 'type; - } payee; - - record { + json...; + |}[] coding; + json...; + |} 'type; + json...; + |} payee; + + record {| string reference; - } provider; + json...; + |} provider; - record { + record {| string reference; - } prescription; + json...; + |} prescription; - record { + record {| string reference; - } patient; + json...; + |} patient; - record { + record {| string reference; - } insurer; + json...; + |} insurer; string id; - record { + record {| string div; string status; - } text; + json...; + |} text; - record { + record {| int sequence; - record { + record {| string reference; - } provider; - }[] careTeam; + json...; + |} provider; + json...; + |}[] careTeam; string resourceType; string status; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_9.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_9.bal index dc93c9f3e0c7..4d51b00dfd50 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_9.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_9.bal @@ -1,95 +1,114 @@ -type Coverage record { +type Coverage record {| string reference; -}; + json...; +|}; -type Identifier record { +type Identifier record {| string system; string value; -}; + json...; +|}; -type InsuranceItem record { +type InsuranceItem record {| Coverage coverage; int sequence; Identifier identifier; boolean focal; -}; + json...; +|}; -type IdentifierItem record { +type IdentifierItem record {| string system; string value; -}; + json...; +|}; -type UnitPrice record { +type UnitPrice record {| string currency; decimal value; -}; + json...; +|}; -type CodingItem record { +type CodingItem record {| string code; -}; + json...; +|}; -type ProductOrService record { +type ProductOrService record {| CodingItem[] coding; -}; + json...; +|}; -type Net record { +type Net record {| string currency; decimal value; -}; + json...; +|}; -type ItemItem record { +type ItemItem record {| UnitPrice unitPrice; int sequence; int[] careTeamSequence; ProductOrService productOrService; string servicedDate; Net net; -}; + json...; +|}; -type DiagnosisCodeableConcept record { +type DiagnosisCodeableConcept record {| CodingItem[] coding; -}; + json...; +|}; -type DiagnosisItem record { +type DiagnosisItem record {| int sequence; DiagnosisCodeableConcept diagnosisCodeableConcept; -}; + json...; +|}; -type Type record { +type Type record {| CodingItem[] coding; -}; + json...; +|}; -type Priority record { +type Priority record {| CodingItem[] coding; -}; + json...; +|}; -type Payee record { +type Payee record {| Type 'type; -}; + json...; +|}; -type Provider record { +type Provider record {| string reference; -}; + json...; +|}; -type Patient record { +type Patient record {| string reference; -}; + json...; +|}; -type Insurer record { +type Insurer record {| string reference; -}; + json...; +|}; -type Text record { +type Text record {| string div; string status; -}; + json...; +|}; -type CareTeamItem record { +type CareTeamItem record {| int sequence; Provider provider; -}; + json...; +|}; -type NewRecord record { +type NewRecord record {| InsuranceItem[] insurance; IdentifierItem[] identifier; ItemItem[] item; @@ -107,4 +126,5 @@ type NewRecord record { CareTeamItem[] careTeam; string resourceType; string status; -}; + json...; +|}; diff --git a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_9_type_desc.bal b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_9_type_desc.bal index 9dc5205457ba..f3e16c24a039 100644 --- a/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_9_type_desc.bal +++ b/misc/ls-extensions/modules/json-to-record-converter/src/test/resources/ballerina/sample_9_type_desc.bal @@ -1,109 +1,135 @@ -type Person record { - record { +type Person record {| + record {| - record { + record {| string reference; - } coverage; + json...; + |} coverage; int sequence; - record { + record {| string system; string value; - } identifier; + json...; + |} identifier; boolean focal; - }[] insurance; + json...; + |}[] insurance; - record { + record {| string system; string value; - }[] identifier; + json...; + |}[] identifier; - record { + record {| - record { + record {| string currency; decimal value; - } unitPrice; + json...; + |} unitPrice; int sequence; int[] careTeamSequence; - record { - record { + record {| + record {| string code; - }[] coding; - } productOrService; + json...; + |}[] coding; + json...; + |} productOrService; string servicedDate; - record { + record {| string currency; decimal value; - } net; - }[] item; + json...; + |} net; + json...; + |}[] item; string use; string created; - record { + record {| int sequence; - record { - record { + record {| + record {| string code; - }[] coding; - } diagnosisCodeableConcept; - }[] diagnosis; - - record { - record { + json...; + |}[] coding; + json...; + |} diagnosisCodeableConcept; + json...; + |}[] diagnosis; + + record {| + record {| string system; string code; - }[] coding; - } 'type; + json...; + |}[] coding; + json...; + |} 'type; - record { - record { + record {| + record {| string code; - }[] coding; - } priority; - - record { - record { - record { + json...; + |}[] coding; + json...; + |} priority; + + record {| + record {| + record {| string code; - }[] coding; - } 'type; - } payee; - - record { + json...; + |}[] coding; + json...; + |} 'type; + json...; + |} payee; + + record {| string reference; - } provider; + json...; + |} provider; - record { + record {| string reference; - } patient; + json...; + |} patient; - record { + record {| string reference; - } insurer; + json...; + |} insurer; string id; - record { + record {| string div; string status; - } text; + json...; + |} text; - record { + record {| int sequence; - record { + record {| string reference; - } provider; - }[] careTeam; + json...; + |} provider; + json...; + |}[] careTeam; string resourceType; string status; -}; + json...; +|};