-
Notifications
You must be signed in to change notification settings - Fork 5
CASL-1785: fix HttpStorageProperties bug #575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| */ | ||
| package com.here.naksha.storage.http; | ||
|
|
||
| import naksha.base.JvmMapProxy; | ||
| import naksha.model.NakshaError; | ||
| import naksha.model.NakshaException; | ||
| import naksha.model.NakshaVersion; | ||
|
|
@@ -35,6 +36,16 @@ | |
| @AvailableSince(NakshaVersion.v2_0_12) | ||
| public class HttpStorageProperties extends NakshaProperties { | ||
|
|
||
| static final class HeaderMap extends JvmMapProxy<String, String> { | ||
| private HeaderMap() { | ||
| super(String.class, String.class); | ||
| } | ||
|
|
||
| private void putHeaders(@NotNull Map<String, String> headers) { | ||
| headers.forEach(this::put); | ||
| } | ||
| } | ||
|
|
||
| public static final Integer DEF_CONNECTION_TIMEOUT_SEC = 20; | ||
| public static final Integer DEF_SOCKET_TIMEOUT_SEC = 90; | ||
| public static final Integer DEF_MAX_RETRIES = 1; | ||
|
|
@@ -105,11 +116,29 @@ public void setMaxRetries(final @Nullable Integer maxRetries) { | |
| * By default: 'Content-Type: application/json' and 'Accept-Encoding: gzip' | ||
| */ | ||
| public @NotNull Map<String, String> getHeaders() { | ||
| return getOrSet(HEADERS, DEFAULT_HEADERS); | ||
| final Object raw = get(HEADERS); | ||
| if (raw instanceof HeaderMap) { | ||
| return (HeaderMap) raw; | ||
| } | ||
| if (raw instanceof Map<?, ?>) { | ||
| HeaderMap headers = toHeaderMap((Map<?, ?>) raw); | ||
| setRaw(HEADERS, headers); | ||
| return headers; | ||
| } | ||
| HeaderMap headers = new HeaderMap(); | ||
| headers.putHeaders(DEFAULT_HEADERS); | ||
| setRaw(HEADERS, headers); | ||
| return headers; | ||
| } | ||
|
|
||
| public void setHeaders(final @Nullable Map<String, String> headers) { | ||
| setRaw(HEADERS, headers); | ||
| if (headers == null) { | ||
| setRaw(HEADERS, null); | ||
| return; | ||
| } | ||
| HeaderMap headerMap = new HeaderMap(); | ||
| headerMap.putHeaders(headers); | ||
| setRaw(HEADERS, headerMap); | ||
| } | ||
|
|
||
| public @NotNull HttpInterface getProtocol() { | ||
|
|
@@ -134,4 +163,14 @@ public void setHeaders(final @Nullable Map<String, String> headers) { | |
|
|
||
| public void setProtocol(final HttpInterface protocol) {setRaw(HTTP_INTERFACE, protocol);} | ||
|
|
||
| private @NotNull HeaderMap toHeaderMap(@NotNull Map<?, ?> rawHeaders) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the earlier comment about " |
||
| HeaderMap headers = new HeaderMap(); | ||
| rawHeaders.forEach((key, value) -> { | ||
| if (key instanceof String && value instanceof String) { | ||
| headers.put((String) key, (String) value); | ||
| } | ||
| }); | ||
| return headers; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
.../jvmTest/resources/unit_test_data/HttpStorageProperties/t05_testBoxStorageProperties.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "id": "http-storage", | ||
| "type": "Storage", | ||
| "className": "com.here.naksha.storage.http.HttpStorage", | ||
| "properties": { | ||
| "url": "https://example.org", | ||
| "headers": { | ||
| "Authorization": "Bearer boxed-token", | ||
| "X-Tenant": "demo" | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IntelliJ complains that it is always false. Meaning, it will never to
Map<?, ?>. Which seems true. Pls revisit and remove this condition if not needed.In that case, the earlier check of
raw instanceof HeaderMapis also not needed. It will either benullor of typeHeaderMap, so just checking fornullshould be sufficient?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I vote for keep these checks. The Kotlin Multiplatform build is not the most straightforward when it comes to IDE errors/warnings as I have seen.
In this case for example I can wrongly do
setRaw(HEADERS, "12");somewhere, and that string would still be stored. So correcting the headers here at getter looks to be the only solution without interfering with the underlyingMapProxy(ideally I prefer to somehow catch this right when it was set).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way I am thinking, creates a doubt - if there is a possibility where
setRaw(HEADERS, "12");can happen during runtime? In the code, I don't see such case. Help with one example?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One example would be
new HttpStorageProperties().setRaw(<<any Object>>), by mistake somewhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you both are correct. with setRaw() and getRaw() we lose encapsulation but it's design of proxy model. Also Map check looks like a lot of boilerplate, and in normal flow it would be unnecessary, also Intelij is sometimes confused by proxy model. What is happening in that case: In most used flow in Storage, Handler etc jsons. When we want access properties we use
JvmBoxingUtil.box(eventHandlerConfig.getProperties(), DefaultStorageHandlerProperties.class));and in our caseHttpStorageProperties storageProperties = JvmBoxingUtil.box(storage.getProperties(), HttpStorageProperties.class);Now when properties has nested objects like in our case Map. In boxing process it's becomeAnyObjectby proxy model. In normal use case we then just call gets on our properties, if we don't haveMap<?,?>check in get. It would failHeaderMapcheck and create and return default headers, so we need step to translate our AnyObject to our wrapper in this caseHeaderMap extends JvmMapProxy<String, String>. I personally think and from I remember Kuba agreed, that proxy model it's a bit complicated and confusing to use for new users, also creating what normally would be POJO becomes complicated and time consuming tasks, like in this example of which we're talking right now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also added test
shouldPreserveHeadersWhenBoxingStoragePropertiesFromJsonto help understand what is going on.