Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Look up docstring converter by type as fallback #2459

Merged
merged 1 commit into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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