osiam / connector4java

Native Java API to connect to the REST based OSIAM services
MIT License
8 stars 17 forks source link

Fix broken immutability of resources #206

Closed tkrille closed 8 years ago

tkrille commented 8 years ago

It was possible to change resources, that should actually be immutable, by using the copy-of constructor of the respective builder class. A simple test case that was working:

def 'Can change immutable User'() {
    given:
    def notSoImmutableUser = new User.Builder('tester').build()

    when:
    new User.Builder(notSoImmutableUser)
            .addExtension(new Extension.Builder('urn:tester').build())

    then:
    notSoImmutableUser.getSchemas().contains('urn:tester')
}

This is also true for all other collection-based attributes, like emails, photos, etc.

Fix this behavior by:

  1. Make a defensive copy of all collections in the resource's constructor. Make this copy an ImmutableList/ImmutableSet. This way, only immutable objects are stored in the resource.
  2. Copy all elements of collection-based attributes in the copy-of constructor of the builders into the builders' own collection.

Remove the creation of ImmutableList/ImmutableSet in the getters, as this is not necessary anymore. All fields store immutable objects now.

This PR additionally contains a bunch of small refactorings. See the individual commits for details:

tkrille commented 8 years ago

I've updated the changelog.

tkrille commented 8 years ago

This has been updated with the resolutions of the comments in #207.

wallner commented 8 years ago

Thank You!