Skip to content

fix: make URLToStringConversion generate URI.create(String).toURL() instead of new URL(String) - #4044

Merged
filiphr merged 1 commit into
mapstruct:mainfrom
yvasyliev:bugfix/string-to-url-conversion
May 31, 2026
Merged

fix: make URLToStringConversion generate URI.create(String).toURL() instead of new URL(String)#4044
filiphr merged 1 commit into
mapstruct:mainfrom
yvasyliev:bugfix/string-to-url-conversion

Conversation

@yvasyliev

Copy link
Copy Markdown
Contributor

This pull request refactors the handling of URL conversions in the codebase, standardizing the approach by using URI.create(...).toURL() instead of direct new URL(...) construction. This change affects both the internal conversion logic and the related tests, aiming for improved consistency and reliability in URL handling.

Refactoring of URL Conversion Logic:

  • The ConversionUtils.url method and related imports of java.net.URL were removed, and all references were updated to use URI and the ConversionUtils.uri method instead.
  • In URLToStringConversion.java, the conversion now constructs URLs using URI.create(<SOURCE>).toURL() rather than new URL(<SOURCE>). This also updates the import types from URL to URI.

Test Adjustments for New Conversion Approach:

  • In URLConversionTest.java, all usages of new URL(...) were replaced with URI.create(...).toURL() to match the new conversion logic.
  • Test cases for invalid URL strings were updated to use malformed URI schemes (e.g., "xxxxxx://mapstruct.org/") instead of arbitrary invalid strings, ensuring the tests accurately reflect the new conversion path.

Closes #4041

@hduelme

hduelme commented May 31, 2026

Copy link
Copy Markdown
Contributor

@yvasyliev In general, I agree that URI.create(...).toURL() should be preferred. However, changing the current implementation has some implications. Instead of always throwing a MalformedURLException, it may now throw anIllegalArgumentException. Additionally, URL would no longer be imported automatically, which could break existing expression blocks.

We'll discuss internally whether to include this change in the upcoming release.
@filiphr, what do you think?

@filiphr

filiphr commented May 31, 2026

Copy link
Copy Markdown
Member

@hduelme I think that it is actually fine to do this. If using the constructor is not the recommended way of creating URLs then we should not stand in the way. We should only mention this in our release notes.

However, changing the current implementation has some implications. Instead of always throwing a MalformedURLException, it may now throw an IllegalArgumentException

People will always have the option of writing their own mapping if they want to keep this behaviour

Additionally, URL would no longer be imported automatically, which could break existing expression blocks.

That's completely fine @hduelme. People should not be relying on how we generate the code for their expressions. We also have Mapper#imports that people can use to import what they need always.

So I would say that we should go ahead and merge this

@filiphr
filiphr merged commit 453602e into mapstruct:main May 31, 2026
7 of 8 checks passed
@yvasyliev
yvasyliev deleted the bugfix/string-to-url-conversion branch May 31, 2026 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

URL conversion uses deprecated constructor new URL(String) in Java 20+

3 participants