Skip to content

Commit

Permalink
[Core] Look up docstring converter by type as fallback
Browse files Browse the repository at this point in the history
Fixes: #2458
  • Loading branch information
mpkorstanje committed Jan 7, 2022
1 parent 0364976 commit 47bdb92
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 173 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Removed

### Fixed
* [Core] Look up docstring converter by type as fallback ([#2459](https://github.com/cucumber/cucumber-jvm/pull/2459) M.P. Korstanje)

## [7.2.1] (2022-01-04)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

import io.cucumber.docstring.DocString;

import static java.util.Objects.requireNonNull;

public final class DocStringArgument implements Argument {

private final DocStringTransformer<?> docStringType;
private final String content;
private final String contentType;

DocStringArgument(DocStringTransformer<?> docStringType, String content, String contentType) {
this.docStringType = docStringType;
this.content = content;
this.docStringType = requireNonNull(docStringType);
this.content = requireNonNull(content);
this.contentType = contentType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
import java.util.ArrayList;
import java.util.List;

import static java.util.Objects.requireNonNull;

public final class StepExpression {

private final Expression expression;
private final DocStringTransformer<?> docStringType;
private final RawTableTransformer<?> tableType;

StepExpression(Expression expression, DocStringTransformer<?> docStringType, RawTableTransformer<?> tableType) {
this.expression = expression;
this.docStringType = docStringType;
this.tableType = tableType;
this.expression = requireNonNull(expression);
this.docStringType = requireNonNull(docStringType);
this.tableType = requireNonNull(tableType);
}

public Class<? extends Expression> getExpressionType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ private static String emptyToAnonymous(String contentType) {
}

List<DocStringType> lookup(String contentType, Type type) {
if (contentType == null) {
return lookUpByType(type);
DocStringType docStringType = lookupByContentTypeAndType(orDefault(contentType), type);
if (docStringType != null) {
return Collections.singletonList(docStringType);
}

DocStringType docStringType = lookupByContentTypeAndType(contentType, type);
if (docStringType == null) {
return Collections.emptyList();
}
return Collections.singletonList(docStringType);
return lookUpByType(type);
}

private String orDefault(String contentType) {
return contentType == null ? DEFAULT_CONTENT_TYPE : contentType;
}

private List<DocStringType> lookUpByType(Type type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,19 @@ public <T> T convert(DocString docString, Type targetType) {
targetType.getTypeName()));
}
if (docStringTypes.size() > 1) {
List<String> suggestedContentTypes = suggestedContentTypes(docStringTypes);
if (docString.getContentType() == null) {
throw new CucumberDocStringException(format(
"Multiple converters found for type %s, add one of the following content types to your docstring %s",
targetType.getTypeName(),
suggestedContentTypes));
}
throw new CucumberDocStringException(format(
"Multiple converters found for type %s, add one of the following content types to your docstring %s",
"Multiple converters found for type %s, and the content type '%s' did not match any of the registered types %s. Change the content type of the docstring or register a docstring type for '%s'",
targetType.getTypeName(),
suggestedContentTypes(docStringTypes)));
docString.getContentType(),
suggestedContentTypes,
docString.getContentType()));
}

return (T) docStringTypes.get(0).transform(docString.getContent());
Expand All @@ -51,12 +60,14 @@ public <T> T convert(DocString docString, Type targetType) {
private List<String> suggestedContentTypes(List<DocStringType> docStringTypes) {
return docStringTypes.stream()
.map(DocStringType::getContentType)
// Can't use the anonymous content type to resolve
// the ambiguity.
.filter(contentType -> !contentType.isEmpty())
.map(DocStringTypeRegistryDocStringConverter::emptyToAnonymous)
.sorted()
.distinct()
.collect(Collectors.toList());
}

private static String emptyToAnonymous(String contentType) {
return contentType.isEmpty() ? "[anonymous]" : contentType;
}

}
Loading

0 comments on commit 47bdb92

Please sign in to comment.