# API breakage on Block to add attributes

**URL:** <https://forum.xwiki.org/t/api-breakage-on-block-to-add-attributes/11830>\
**Category:** Development\
**Tags:** vote\
**Created:** [February 1, 2023, 4:29pm UTC](https://forum.xwiki.org/t/api-breakage-on-block-to-add-attributes/11830 "2023-02-01T16:29:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MichaelHamann](https://forum.xwiki.org/user_avatar/forum.xwiki.org/michaelhamann/32/3813_2.png) [@MichaelHamann](https://forum.xwiki.org/u/MichaelHamann)\
**Post date:** [February 1, 2023, 4:29pm UTC](https://forum.xwiki.org/t/api-breakage-on-block-to-add-attributes/11830/1 "2023-02-01T16:29:26Z")

</div>

As discussed in [this topic](https://forum.xwiki.org/t/adding-a-generic-internal-attribute-map-to-xdom-blocks/11788), I want to add attribute-related methods to the `Block` interface, a public API. I need this API to fix a security vulnerability and thus this needs to be backported to the LTS branch 14.10.x. The new methods are the same as for parameters, just with `Object` values:

```java
Map<String, Object> getAttributes();
Object getAttribute(String name);
void setAttribute(String name, Object value);
void setAttributes(Map<String, Object> attributes);

```

It is not possible to provide sensible default implementations for these methods as this would require a map to store the attributes. There are two options:

1. Provide a default implementation that emulates an empty, read-only attribute map, i.e., throw `UnsupportedOperationException` for the set-methods and return `null`/an empty map for the get-methods. You can see this currently in [the pull request](https://github.com/xwiki/xwiki-rendering/pull/245).
2. Make this a real API breakage.

Note that in any case, the actual implementation is provided in `AbstractBlock` which is the base class of all existing implementations of `Block`, I would expect that any extension that adds its own `Block` implementation also inherits from it. `AbstractBlock` is also a public API and its documentation explicitly says “All blocks should extend this class.”. Therefore, regardless of the chosen solution, there should be no breakage in practice.

As the default voting period of 72 hours would end during the weekend, I’m extending the vote to February 6, included.

As this is an API breakage, anyways, here is my +0 for option 1 and +1 for option 2.

---

<div class="post-metadata">

**Author:** ![tmortagne](https://forum.xwiki.org/user_avatar/forum.xwiki.org/tmortagne/32/4930_2.png) [@tmortagne](https://forum.xwiki.org/u/tmortagne)\
**Post date:** [February 1, 2023, 4:58pm UTC](https://forum.xwiki.org/t/api-breakage-on-block-to-add-attributes/11830/2 "2023-02-01T16:58:05Z")

</div>

I would prefer option 3:

- default implementation for getters (the current one, emulating an empty map of attributes)
- no implementation for setters, since it’s impossible to implement them. In any case -1 to implement them just to throw an exception indicating that they are not implemented since it does not make any sense, as discussed many times it’s much better to acknowledge that this is an API breakage (and indicate that it’s very unlikely that it will cause a problem since we expect most implementations of Block to extend AbstractBlock)

---

<div class="post-metadata">

**Author:** ![mleduc](https://forum.xwiki.org/user_avatar/forum.xwiki.org/mleduc/32/2328_2.png) [@mleduc](https://forum.xwiki.org/u/mleduc)\
**Post date:** [February 1, 2023, 5:06pm UTC](https://forum.xwiki.org/t/api-breakage-on-block-to-add-attributes/11830/3 "2023-02-01T17:06:47Z")

</div>

> [@tmortagne](#):
>
> I would prefer option 3:
> 
> - default implementation for getters (the current one, emulating an empty map of attributes)
> - no implementation for setters, since it’s impossible to implement them. In any case -1 to implement them just to throw an exception indicating that they are not implemented since it does not make any sense, as discussed many times it’s much better to acknowledge that this is an API breakage (and indicate that it’s very unlikely that it will cause a problem since we expect most implementations of Block to extend AbstractBlock)

+1 for that option

---

<div class="post-metadata">

**Author:** ![mflorea](https://forum.xwiki.org/user_avatar/forum.xwiki.org/mflorea/32/49_2.png) [@mflorea](https://forum.xwiki.org/u/mflorea)\
**Post date:** [February 2, 2023, 5:50am UTC](https://forum.xwiki.org/t/api-breakage-on-block-to-add-attributes/11830/4 "2023-02-02T05:50:52Z")

</div>

Same as Thomas and Manuel.

---

<div class="post-metadata">

**Author:** ![vmassol](https://forum.xwiki.org/user_avatar/forum.xwiki.org/vmassol/32/6_2.png) [@vmassol](https://forum.xwiki.org/u/vmassol)\
**Post date:** [February 6, 2023, 12:44pm UTC](https://forum.xwiki.org/t/api-breakage-on-block-to-add-attributes/11830/5 "2023-02-06T12:44:05Z")

</div>

> [@tmortagne](#):
>
> since it does not make any sense, as discussed many times it’s much better to acknowledge that this is an API breakage

About this, if this is what we prefer, I think we need to update [https://dev.xwiki.org/xwiki/bin/view/Community/DevelopmentPractices#HBackwardCompatibility](https://dev.xwiki.org/xwiki/bin/view/Community/DevelopmentPractices#HBackwardCompatibility) where it says:

> When you need to add a new method to an interface there are 2 solutions to preserve backward compatibility:
> 
> - Create a new interface with the new method and deprecate the old one. This means that code using the old interface must be modified to support the 2 interfaces, and that’s not easy.
> - (Recommended) Use [Default Methods](https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html) (introduced in Java 8).

In view of Thomas’s answer we need to add a sub bullet point to the second bullet point about default methods and explain when it shouldn’t be used and what should be done instead.

---

<div class="post-metadata">

**Author:** ![tmortagne](https://forum.xwiki.org/user_avatar/forum.xwiki.org/tmortagne/32/4930_2.png) [@tmortagne](https://forum.xwiki.org/u/tmortagne)\
**Post date:** [February 6, 2023, 6:21pm UTC](https://forum.xwiki.org/t/api-breakage-on-block-to-add-attributes/11830/6 "2023-02-06T18:21:28Z")

</div>

> [@vmassol](#):
>
> I think we need to update [Development Practices - XWiki](https://dev.xwiki.org/xwiki/bin/view/Community/DevelopmentPractices#HBackwardCompatibility)

By the way, it seems to have different reference on various Java subject in [https://dev.xwiki.org/xwiki/bin/view/Community/DevelopmentPractices](https://dev.xwiki.org/xwiki/bin/view/Community/DevelopmentPractices) and [https://dev.xwiki.org/xwiki/bin/view/Community/CodeStyle/JavaCodeStyle/](https://dev.xwiki.org/xwiki/bin/view/Community/CodeStyle/JavaCodeStyle/) (and maybe others [https://dev.xwiki.org/xwiki/bin/view/Community/CodeStyle/](https://dev.xwiki.org/xwiki/bin/view/Community/CodeStyle/)).

---

<div class="post-metadata">

**Author:** ![vmassol](https://forum.xwiki.org/user_avatar/forum.xwiki.org/vmassol/32/6_2.png) [@vmassol](https://forum.xwiki.org/u/vmassol)\
**Post date:** [February 7, 2023, 9:55am UTC](https://forum.xwiki.org/t/api-breakage-on-block-to-add-attributes/11830/7 "2023-02-07T09:55:59Z")

</div>

> [@tmortagne](#):
>
> By the way, it seems to have different reference on various Java subject in [Development Practices - XWiki](https://dev.xwiki.org/xwiki/bin/view/Community/DevelopmentPractices) and [Java Code Style - XWiki](https://dev.xwiki.org/xwiki/bin/view/Community/CodeStyle/JavaCodeStyle/) (and maybe others [Code Style - XWiki](https://dev.xwiki.org/xwiki/bin/view/Community/CodeStyle/)).

The original idea was the following:

- [https://dev.xwiki.org/xwiki/bin/view/Community/DevelopmentPractices](https://dev.xwiki.org/xwiki/bin/view/Community/DevelopmentPractices) is the main page and contains all dev practices
- It has a section about coding style at [https://dev.xwiki.org/xwiki/bin/view/Community/DevelopmentPractices#HCodingConventions](https://dev.xwiki.org/xwiki/bin/view/Community/DevelopmentPractices#HCodingConventions)
- Coding styles have they own page as there are a lot of them.

What has happened I think is that more topics got added to [https://dev.xwiki.org/xwiki/bin/view/Community/CodeStyle/JavaCodeStyle/](https://dev.xwiki.org/xwiki/bin/view/Community/CodeStyle/JavaCodeStyle/) and some of these topics are not about coding styles. The idea of the coding style pages were about the code formatting that you have in your IDEs (spaces, tabs, how many, new lines, etc).

So I think we need to move some content from [https://dev.xwiki.org/xwiki/bin/view/Community/CodeStyle/JavaCodeStyle/](https://dev.xwiki.org/xwiki/bin/view/Community/CodeStyle/JavaCodeStyle/) to [https://dev.xwiki.org/xwiki/bin/view/Community/DevelopmentPractices](https://dev.xwiki.org/xwiki/bin/view/Community/DevelopmentPractices) and since that page is getting large too, to split it into several subpages.

I’ll try to propose something.

---

<div class="post-metadata">

**Author:** ![MichaelHamann](https://forum.xwiki.org/user_avatar/forum.xwiki.org/michaelhamann/32/3813_2.png) [@MichaelHamann](https://forum.xwiki.org/u/MichaelHamann)\
**Post date:** [February 16, 2023, 12:55pm UTC](https://forum.xwiki.org/t/api-breakage-on-block-to-add-attributes/11830/8 "2023-02-16T12:55:14Z")

</div>

In the end I decided to not to use `Block` attributes so I’m not going to perform this change and there will be no breakage. This vote has 3 +1 for option 3 so option 3 passes the vote just in case somebody else needs block attributes at a later point.

Note also that in the meantime I discovered that `CompositeBlock` allows storing string parameters that are ignored by renderers. It would also be possible to add similar blocks that allow storing custom values without affecting rendering (some code might not ignore them though when, e.g., analyzing the XDOM for certain patterns).
