OptionStyleProvider bug for SuggestionPickerField

Hi

In documentation, OptionStyleProvider is explicitely described for LookupField.

However in Studio it is also proposed as a component handler for SuggestionPickerField.

image

But if you try to implement it, for instance using code similar to the one proposed in the sampler.

 @Install(to = "customerSuggestionPickerField", subject = "optionStyleProvider")
    private String customerSuggestionPickerFieldOptionStyleProvider(Customer customer) {
        if (customer.getCreditReputation() == null)
            return null;
        switch (customer.getCreditReputation()) {
            case BAD:
                return "bad-rating";
            case WARNING:
                return "warning-rating";
            default:
                return null;
        }
    }

Then you will have a weird behavior when entering input in the field.

Exception in thread "BackgroundTask-1-admin" java.lang.RuntimeException: Push failed
	at com.vaadin.server.communication.AtmospherePushConnection.push(AtmospherePushConnection.java:167)
	at com.vaadin.server.communication.AtmospherePushConnection.push(AtmospherePushConnection.java:142)
	at com.vaadin.ui.UI.push(UI.java:1708)
	at com.vaadin.server.VaadinSession.unlock(VaadinSession.java:1018)
	at com.vaadin.server.VaadinService.ensureAccessQueuePurged(VaadinService.java:2055)
	at com.vaadin.server.VaadinService.accessSession(VaadinService.java:2021)
	at com.vaadin.server.VaadinSession.access(VaadinSession.java:1409)
	at com.vaadin.ui.UI.access(UI.java:1572)
	at com.haulmont.cuba.web.gui.executors.impl.WebBackgroundWorker$WebTaskExecutor$1.done(WebBackgroundWorker.java:200)
	at java.base/java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:381)
	at java.base/java.util.concurrent.FutureTask.set(FutureTask.java:232)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:272)
	at com.haulmont.cuba.web.gui.executors.impl.WebBackgroundWorker$WebTaskExecutor.lambda$startExecution$1(WebBackgroundWorker.java:390)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException
	at elemental.json.impl.JsonUtil.quote(JsonUtil.java:208)
	at elemental.json.impl.JsonUtil$StringifyJsonVisitor.visit(JsonUtil.java:96)
	at elemental.json.impl.JreJsonString.traverse(JreJsonString.java:81)
	at elemental.json.impl.JsonVisitor.accept(JsonVisitor.java:82)
	at elemental.json.impl.JreJsonObject.traverse(JreJsonObject.java:177)
	at elemental.json.impl.JsonVisitor.accept(JsonVisitor.java:82)
	at elemental.json.impl.JreJsonArray.traverse(JreJsonArray.java:164)
	at elemental.json.impl.JsonVisitor.accept(JsonVisitor.java:82)
	at elemental.json.impl.JreJsonArray.traverse(JreJsonArray.java:164)
	at elemental.json.impl.JsonVisitor.accept(JsonVisitor.java:82)
	at elemental.json.impl.JreJsonArray.traverse(JreJsonArray.java:164)
	at elemental.json.impl.JsonVisitor.accept(JsonVisitor.java:82)
	at elemental.json.impl.JreJsonArray.traverse(JreJsonArray.java:164)
	at elemental.json.impl.JsonVisitor.accept(JsonVisitor.java:82)
	at elemental.json.impl.JsonVisitor.accept(JsonVisitor.java:72)
	at elemental.json.impl.JsonUtil.stringify(JsonUtil.java:281)
	at elemental.json.impl.JsonUtil.stringify(JsonUtil.java:267)
	at elemental.json.impl.JsonUtil.stringify(JsonUtil.java:252)
	at com.vaadin.server.communication.ClientRpcWriter.write(ClientRpcWriter.java:106)
	at com.vaadin.server.communication.UidlWriter.write(UidlWriter.java:207)
	at com.vaadin.server.communication.AtmospherePushConnection.push(AtmospherePushConnection.java:164)
	... 15 more

Caused by the return of null.

I guess it should be either implemented, or not proposed by Studio as a component handler.

Regards
Michaël

Hi,

Both SuggestionField and SuggestionPickerField support OptionStyleProvider. The problem appears because you return null in case you don’t want to add a custom stylename. Replace null with an empty string.

Regards,
Gleb

Thanks @gorelov.

I see that the sampler has been updated to provide an example of OptionsStyleProvider for SuggestionField and SuggestionPickerField.

However there is still an inconcistency that may trouble developers. OptionsStyleProvider accepts null as a return for LookupField whereas SuggestionField and SuggestionPickerField do not.

See the sampler code for LookupField reproduced below.

@Install(to = "lookupField", subject = "optionStyleProvider")
    protected String lookupFieldOptionStyleProvider(CustomerGrade grade) {
        if (grade != null) {
            switch (grade) {
                case HIGH:
                    return "high-grade";
                case PREMIUM:
                    return "premium-grade";
                case STANDARD:
                    return "standard-grade";
            }
        }
        return null;
    }

One having seen once the sample code for LookupField will probably never go back to the sampler in order to look other samples for the same feature, assuming they work identically. Which is valid rationale.

So ideally the behavior between these fields should be aligned at some point.

Anyway, thanks for the hint.

Regards
Michael

1 Like