From 6630a16fae788a85aba830b7167be7994880ac6a Mon Sep 17 00:00:00 2001 From: lnash94 Date: Fri, 15 Nov 2024 17:03:17 +0530 Subject: [PATCH] Sanitized functions parameters --- .../service/mapper/type/RecordTypeMapper.java | 99 ++++++++++++++----- .../service/mapper/type/TypeMapperImpl.java | 7 +- 2 files changed, 77 insertions(+), 29 deletions(-) diff --git a/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/type/RecordTypeMapper.java b/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/type/RecordTypeMapper.java index f8b34c76d..a263bc985 100644 --- a/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/type/RecordTypeMapper.java +++ b/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/type/RecordTypeMapper.java @@ -85,9 +85,10 @@ public static Schema getSchema(RecordTypeSymbol typeSymbol, Components component Map recordFieldMap = new LinkedHashMap<>(typeSymbol.fieldDescriptors()); List allOfSchemaList = mapIncludedRecords(typeSymbol, components, recordFieldMap, additionalData, recordName, fieldsOnlyForRequiredList); - - Map properties = mapRecordFields(recordFieldMap, components, requiredFields, - recordName, false, additionalData, fieldsOnlyForRequiredList); + RecordFieldMappingContext mappingContext = new RecordFieldMappingContext( + recordFieldMap, components, requiredFields, recordName, false, + true, additionalData, fieldsOnlyForRequiredList); + Map properties = mapRecordFields(mappingContext); Optional restFieldType = typeSymbol.restTypeDescriptor(); if (restFieldType.isPresent()) { @@ -139,22 +140,29 @@ static List mapIncludedRecords(RecordTypeSymbol typeSymbol, Components c if (!includedRecordFieldValue.typeDescriptor().equals(recordFieldSymbol.typeDescriptor())) { continue; } - eliminateRedundantFields(recordFieldMap, additionalData, recordName, typeInclusion, - includedRecordField, recordFieldSymbol, includedRecordFieldValue, - fieldsOnlyForRequiredList); + IncludedFieldContext context = new IncludedFieldContext( + recordFieldMap, + recordName, + typeInclusion, + includedRecordField, + recordFieldSymbol, + includedRecordFieldValue + ); + eliminateRedundantFields(context, additionalData, fieldsOnlyForRequiredList); } } } return allOfSchemaList; } - private static void eliminateRedundantFields(Map recordFieldMap, - AdditionalData additionalData, String recordName, - TypeSymbol typeInclusion, - Map.Entry includedRecordField, - RecordFieldSymbol recordFieldSymbol, - RecordFieldSymbol includedRecordFieldValue, + private static void eliminateRedundantFields(IncludedFieldContext context, AdditionalData additionalData, Set fieldsOnlyForRequiredList) { + Map recordFieldMap = context.recordFieldMap; + String recordName = context.recordName; + TypeSymbol typeInclusion = context.typeInclusion; + Map.Entry includedRecordField = context.includedRecordField; + RecordFieldSymbol recordFieldSymbol = context.recordFieldSymbol; + RecordFieldSymbol includedRecordFieldValue = context.includedRecordFieldValue; boolean recordHasDefault = recordFieldSymbol.hasDefaultValue(); boolean includedHasDefault = includedRecordFieldValue.hasDefaultValue(); @@ -218,13 +226,59 @@ private static void eliminateRedundantFields(Map reco } } - public static Map mapRecordFields(Map recordFieldMap, - Components components, Set requiredFields, - String recordName, boolean treatNilableAsOptional, - boolean inferNameFromJsonData, - AdditionalData additionalData, - Set fieldsOnlyForRequiredList) { + /** + * Encapsulates the context of included fields in a record for processing. + * + * @param recordFieldMap A map containing record field symbols. + * @param recordName The name of the record being processed. + * @param typeInclusion The type symbol representing type inclusions in the record. + * @param includedRecordField An entry representing the included record field and its symbol. + * @param recordFieldSymbol The symbol of the current record field being processed. + * @param includedRecordFieldValue The symbol of the field in the included record. + */ + public record IncludedFieldContext( + Map recordFieldMap, + String recordName, + TypeSymbol typeInclusion, + Map.Entry includedRecordField, + RecordFieldSymbol recordFieldSymbol, + RecordFieldSymbol includedRecordFieldValue) { + } + + /** + * Encapsulates the context needed for mapping record fields to schemas. + * + * @param recordFieldMap A map containing record field symbols. + * @param components Components used for managing and storing schemas during mapping. + * @param requiredFields A set of field names that are required in the mapped schema. + * @param recordName The name of the record being processed. + * @param treatNilableAsOptional Flag indicating whether nilable fields should be treated as optional. + * @param inferNameFromJsonData Flag indicating whether field names should be inferred from JSON data. + * @param additionalData Additional data required for schema generation and field processing. + * @param fieldsOnlyForRequiredList A set of fields that should be exclusively marked as required. + */ + public record RecordFieldMappingContext( + Map recordFieldMap, + Components components, + Set requiredFields, + String recordName, + boolean treatNilableAsOptional, + boolean inferNameFromJsonData, + AdditionalData additionalData, + Set fieldsOnlyForRequiredList) { + } + + public static Map mapRecordFields(RecordFieldMappingContext context) { + Map recordFieldMap = context.recordFieldMap; + Components components = context.components; + Set requiredFields = context.requiredFields; + String recordName = context.recordName; + boolean treatNilableAsOptional = context.treatNilableAsOptional; + boolean inferNameFromJsonData = context.inferNameFromJsonData; + AdditionalData additionalData = context.additionalData; + Set fieldsOnlyForRequiredList = context.fieldsOnlyForRequiredList; Map properties = new LinkedHashMap<>(); + for (Map.Entry recordField : recordFieldMap.entrySet()) { RecordFieldSymbol recordFieldSymbol = recordField.getValue(); String recordFieldName = getRecordFieldName(inferNameFromJsonData, recordField, @@ -258,15 +312,6 @@ public static Map mapRecordFields(Map return properties; } - public static Map mapRecordFields(Map recordFieldMap, - Components components, Set requiredFields, - String recordName, boolean treatNilableAsOptional, - AdditionalData additionalData, - Set fieldsOnlyForRequiredList) { - return mapRecordFields(recordFieldMap, components, requiredFields, recordName, treatNilableAsOptional, - true, additionalData, fieldsOnlyForRequiredList); - } - private static String getRecordFieldName(boolean inferNameFromJsonData, Map.Entry recordFieldEntry, SemanticModel semanticModel) { diff --git a/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/type/TypeMapperImpl.java b/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/type/TypeMapperImpl.java index d2a3399c9..160e6243c 100644 --- a/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/type/TypeMapperImpl.java +++ b/ballerina-to-openapi/src/main/java/io/ballerina/openapi/service/mapper/type/TypeMapperImpl.java @@ -114,8 +114,11 @@ protected static void createComponentMapping(TypeReferenceTypeSymbol typeSymbol, public Map getSchemaForRecordFields(Map recordFieldMap, Set requiredFields, String recordName, boolean treatNilableAsOptional) { - return RecordTypeMapper.mapRecordFields(recordFieldMap, components, requiredFields, recordName, - treatNilableAsOptional, false, componentMapperData, new HashSet<>()); + + RecordTypeMapper.RecordFieldMappingContext context = new RecordTypeMapper.RecordFieldMappingContext( + recordFieldMap, components, requiredFields, recordName, treatNilableAsOptional, + false, componentMapperData, new HashSet<>()); + return RecordTypeMapper.mapRecordFields(context); } public TypeSymbol getReferredType(TypeSymbol typeSymbol) {