spring-projects / spring-projects/spring-ai

Improve robustness and readability in CosmosDBFilterExpressionConverter

Open
#5,337 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

I would like to propose a small improvement to the CosmosDBFilterExpressionConverter to make it more robust and idiomatic.

The changes focus on:
1.Improving constructor robustness
2.Simplifying and clarifying the doKey method implementation

  1. Constructor Robustness
    Currently, the constructor directly collects the provided columns into a Map using Collectors.toMap.
package org.springframework.ai.vectorstore.cosmosdb;

class CosmosDBFilterExpressionConverter extends AbstractFilterExpressionConverter {
	private Map<String, String> metadataFields;

	CosmosDBFilterExpressionConverter(Collection<String> columns) {
		this.metadataFields = columns.stream().collect(Collectors.toMap(Function.identity(),                    
            Function.identity())); 
	}


Issues
If the input Collection contains duplicate values, Collectors.toMap throws an IllegalStateException.
There is no explicit validation for a null input.

Proposed improvement
Add Assert.notNull to fail fast on invalid input.
Apply distinct() to safely ignore duplicate column names.

CosmosDBFilterExpressionConverter(Collection<String> columns) {
    Assert.notNull(columns, "Columns must not be null");
    this.metadataFields = columns.stream()
            .distinct()
            .collect(Collectors.toMap(Function.identity(), Function.identity()));
}

2.Refactoring doKey Method

The current implementation relies on an Optional.isPresent() check followed by Optional.get().

	@Override
	protected void doKey(Key key, StringBuilder context) {
		String keyName = key.key();
		Optional<String> metadataField = getMetadataField(keyName);
		if (metadataField.isPresent()) {
			context.append("c.metadata." + metadataField.get());
		}
		else {
			throw new IllegalArgumentException(String.format("No metadata field %s has been configured", 
             keyName));
		}
	}


Problem:
This pattern is more verbose and less idiomatic than modern Optional usage.

Proposed improvement:
Use orElseThrow to clearly express that a missing metadata field is an exceptional case


@Override
protected void doKey(Key key, StringBuilder context) {
    String keyName = key.key();

    String field = getMetadataField(keyName)
        .orElseThrow(() -> new IllegalArgumentException(
            String.format("No metadata field %s has been configured", keyName)));

    context.append("c.metadata.").append(field);
}

Benefits:
Clearer control flow
No unsafe use of Optional.get()
Improved readability and maintainability

Action
If these changes look reasonable, I would be happy to submit a Pull Request.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Locate CosmosDBFilterExpressionConverter and inspect its constructor and doKey method, the two entry points named in the issue. Check how metadataFields and getMetadataField are used before making the requested robustness and Optional-handling changes. Done means duplicate columns no longer fail, null input is rejected, and missing metadata fields still produce the stated exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
databases
Issue type
Refactor
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.