Skip to content

Base Schema builder is returned in some builder methods.Β #303

Open
@Amartel1986

Description

@Amartel1986

When trying to build a concrete schema using it's builder I met a strange situation when methods calling order matters because of their return types.

For example this code is valid:

        StringSchema stringValue = StringSchema.builder()
            .maxLength(10)
            .title("String value")
            .build();

while this is not:

        StringSchema stringValue = StringSchema.builder()
            .title("String value")
            .maxLength(10)
            .build();

This feels like a bug to me.

One of the possible solutions is to use a concrete builder as a generic parameter:

    public abstract class Schema {
        public abstract static class Builder<S extends Schema, B extends Builder> {
            public B title(String title) {
                this.title = title;
                //noinspection unchecked
                return (B) this;
            }
        }
    }

    public class StringSchema extends Schema {
        public static class Builder extends Schema.Builder<StringSchema, Builder> {

        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions