Skip to content

Commit

Permalink
Sanitized functions parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
lnash94 committed Nov 15, 2024
1 parent 1f950bd commit 6630a16
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ public static Schema getSchema(RecordTypeSymbol typeSymbol, Components component
Map<String, RecordFieldSymbol> recordFieldMap = new LinkedHashMap<>(typeSymbol.fieldDescriptors());
List<Schema> allOfSchemaList = mapIncludedRecords(typeSymbol, components, recordFieldMap, additionalData,
recordName, fieldsOnlyForRequiredList);

Map<String, Schema> properties = mapRecordFields(recordFieldMap, components, requiredFields,
recordName, false, additionalData, fieldsOnlyForRequiredList);
RecordFieldMappingContext mappingContext = new RecordFieldMappingContext(
recordFieldMap, components, requiredFields, recordName, false,
true, additionalData, fieldsOnlyForRequiredList);
Map<String, Schema> properties = mapRecordFields(mappingContext);

Optional<TypeSymbol> restFieldType = typeSymbol.restTypeDescriptor();
if (restFieldType.isPresent()) {
Expand Down Expand Up @@ -139,22 +140,29 @@ static List<Schema> 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<String, RecordFieldSymbol> recordFieldMap,
AdditionalData additionalData, String recordName,
TypeSymbol typeInclusion,
Map.Entry<String, RecordFieldSymbol> includedRecordField,
RecordFieldSymbol recordFieldSymbol,
RecordFieldSymbol includedRecordFieldValue,
private static void eliminateRedundantFields(IncludedFieldContext context, AdditionalData additionalData,
Set<String> fieldsOnlyForRequiredList) {
Map<String, RecordFieldSymbol> recordFieldMap = context.recordFieldMap;
String recordName = context.recordName;
TypeSymbol typeInclusion = context.typeInclusion;
Map.Entry<String, RecordFieldSymbol> includedRecordField = context.includedRecordField;
RecordFieldSymbol recordFieldSymbol = context.recordFieldSymbol;
RecordFieldSymbol includedRecordFieldValue = context.includedRecordFieldValue;

boolean recordHasDefault = recordFieldSymbol.hasDefaultValue();
boolean includedHasDefault = includedRecordFieldValue.hasDefaultValue();
Expand Down Expand Up @@ -218,13 +226,59 @@ private static void eliminateRedundantFields(Map<String, RecordFieldSymbol> reco
}
}

public static Map<String, Schema> mapRecordFields(Map<String, RecordFieldSymbol> recordFieldMap,
Components components, Set<String> requiredFields,
String recordName, boolean treatNilableAsOptional,
boolean inferNameFromJsonData,
AdditionalData additionalData,
Set<String> 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<String, RecordFieldSymbol> recordFieldMap,
String recordName,
TypeSymbol typeInclusion,
Map.Entry<String, RecordFieldSymbol> 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<String, RecordFieldSymbol> recordFieldMap,
Components components,
Set<String> requiredFields,
String recordName,
boolean treatNilableAsOptional,
boolean inferNameFromJsonData,
AdditionalData additionalData,
Set<String> fieldsOnlyForRequiredList) {
}

public static Map<String, Schema> mapRecordFields(RecordFieldMappingContext context) {
Map<String, RecordFieldSymbol> recordFieldMap = context.recordFieldMap;
Components components = context.components;
Set<String> requiredFields = context.requiredFields;
String recordName = context.recordName;
boolean treatNilableAsOptional = context.treatNilableAsOptional;
boolean inferNameFromJsonData = context.inferNameFromJsonData;
AdditionalData additionalData = context.additionalData;
Set<String> fieldsOnlyForRequiredList = context.fieldsOnlyForRequiredList;
Map<String, Schema> properties = new LinkedHashMap<>();

for (Map.Entry<String, RecordFieldSymbol> recordField : recordFieldMap.entrySet()) {
RecordFieldSymbol recordFieldSymbol = recordField.getValue();
String recordFieldName = getRecordFieldName(inferNameFromJsonData, recordField,
Expand Down Expand Up @@ -258,15 +312,6 @@ public static Map<String, Schema> mapRecordFields(Map<String, RecordFieldSymbol>
return properties;
}

public static Map<String, Schema> mapRecordFields(Map<String, RecordFieldSymbol> recordFieldMap,
Components components, Set<String> requiredFields,
String recordName, boolean treatNilableAsOptional,
AdditionalData additionalData,
Set<String> fieldsOnlyForRequiredList) {
return mapRecordFields(recordFieldMap, components, requiredFields, recordName, treatNilableAsOptional,
true, additionalData, fieldsOnlyForRequiredList);
}

private static String getRecordFieldName(boolean inferNameFromJsonData,
Map.Entry<String, RecordFieldSymbol> recordFieldEntry,
SemanticModel semanticModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ protected static void createComponentMapping(TypeReferenceTypeSymbol typeSymbol,
public Map<String, Schema> getSchemaForRecordFields(Map<String, RecordFieldSymbol> recordFieldMap,
Set<String> 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) {
Expand Down

0 comments on commit 6630a16

Please sign in to comment.