Skip to content

Improve null-safety in Factories and cleanup #53

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

Conversation

ArnyminerZ
Copy link
Member

@ArnyminerZ ArnyminerZ commented Jul 25, 2024

Code cleanup, including new utility functions and a new XmlReader class which simplifies the factory classes.

Signed-off-by: Arnau Mora <arnyminerz@proton.me>
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
@ArnyminerZ ArnyminerZ added the enhancement New feature or request label Jul 25, 2024
@ArnyminerZ ArnyminerZ self-assigned this Jul 25, 2024
@ArnyminerZ ArnyminerZ linked an issue Jul 25, 2024 that may be closed by this pull request
@ArnyminerZ ArnyminerZ marked this pull request as draft July 25, 2024 06:08
@ArnyminerZ ArnyminerZ marked this pull request as ready for review July 25, 2024 07:53
@ArnyminerZ ArnyminerZ requested a review from rfc2822 July 25, 2024 07:53
@rfc2822
Copy link
Member

rfc2822 commented Jul 26, 2024

We should have an approach that isn't specific to PushTopic or another specific element.

Most often the exception is logged because requireReadText() is used, which throws an exception when a property doesn't have a text value, although that can be perfectly valid:

# (taken from RFC 4918 p. 37)
…
<D:propstat>
  <D:prop><R:DingALing/><R:Random/></D:prop>
  <D:status>HTTP/1.1 403 Forbidden</D:status>
</D:propstat>
…

Here we would have the R:DingALing and R:Random elements which could not be parsed when they don't have content, but use requireReadText(), for instance because they usually look like <R:Random>Some Text</R:Random>.

I think it's better to not make any assumptions about the content of elements in dav4jvm (maybe except if explicitly required by the standard, but even then we should parse as forgiving as possible). If at all, this should be left to the application that uses dav4jvm.

So, I suggest to

  • drop requireReadText() because it does things that shouldn't be done (throw exception when contents are unexpected),
  • use readText() everywhere instead,
  • expect all values to be null (like in AddressbookDescription).

So for instance the Push Topic should also have a val topic: String? that can be null.

@rfc2822 rfc2822 added bug Something isn't working and removed enhancement New feature or request labels Jul 26, 2024
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
@ArnyminerZ
Copy link
Member Author

I've deprecated requireReadText instead of removing it, but we can simply get rid of it and call it a day, I don't know how many people is using this library and that utility specifically.

Copy link
Member

@rfc2822 rfc2822 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would solve the existing problem (see #9) that this can't be parsed without information loss:

<multistatus>
… with 404: …
<prop>
  <getetag/>
</prop>
</multistatus> 

to consequently allow empty properties.

So in the case of GetETag, we have to deal with the situation above, so it should be possible to generate an empty GetETag. Who knows, maybe the receiving app needs to list the 404 properties, or maybe there's at some day something like:

<this-server-supports>
  <getetag/>
</this-server-supports>

Then we also need the empty GetETag element.

So, we should parse and return a GetETag in this case, but without content (String value may be null).

Same applies to all elements.

Comment on lines 93 to 96
@Deprecated(
message = "Use readText instead. This function throws exceptions for unexpected contents.",
replaceWith = ReplaceWith("readText(parser)")
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should drop it completely.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same applies to all elements.

Do you mean all? Like, make PropertyFactory.create return non-null? Or just Topic, GetETag and PushSubscribe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example CalendarColor requires an int color, we can always set to black or something like that but I'm not sure if we want that

Copy link
Member

@rfc2822 rfc2822 Jul 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd allow null too for the same reason: It may be interesting for an application to know that an empty element was transmitted.

I have thought about returning some kind of for instance Empty<CalendarColor> when CalendarColor couldn't be parsed with value, but it makes thing unnecessary complex.

The HrefListProperty properties should automatically work. The others often already allow null values. I think we should consequently allow null values in all properties, also to be consistent.

Signed-off-by: Arnau Mora <arnyminerz@proton.me>
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
Signed-off-by: Arnau Mora <arnyminerz@proton.me>
@rfc2822
Copy link
Member

rfc2822 commented Jul 28, 2024

Real-world example of misleading logs from accessing a WebDAV folder in DAVx5:

		<d:propstat>
			<d:prop>
				<d:current-user-privilege-set/>
				<d:getcontenttype/>
				<d:getcontentlength/>
			</d:prop>
			<d:status>HTTP/1.1 404 Not Found</d:status>

because the folder doesn't have a getcontentlength property causes in the logs:

15:06:09.682  8584-8609  at.bitfire.dav4jvm.Property     D  Ignoring unknown property DAV::getcontentlength

although the property is known (but it can be parsed because it currently requires a value).

@ArnyminerZ
Copy link
Member Author

Okay, I'll make all properties nullable then 😉

Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
@ArnyminerZ ArnyminerZ requested a review from rfc2822 July 29, 2024 07:19
Copy link
Member

@rfc2822 rfc2822 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some requests 😃

Also please provide a short PR summary. It can be very short like "Currently there's misleading logging, so now we do this and that: - …, - …", but at least to have an idea what it's about.

And it's often not exactly the same the issue, like in this case. The issue is just about the logging, but the PR is now about null values etc.

Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
Signed-off-by: Arnau Mora Gras <arnyminerz@proton.me>
@ArnyminerZ ArnyminerZ changed the title Changed logging for Factory on missing push topic Improve null-safety and made parser more lenient Aug 2, 2024
@ArnyminerZ ArnyminerZ changed the title Improve null-safety and made parser more lenient Improve null-safety in Factories and cleanup Aug 2, 2024
@ArnyminerZ
Copy link
Member Author

Should be ready. Sorry for the long PR, but there are a lot of classes that use the same methods 😅

@ArnyminerZ ArnyminerZ requested a review from rfc2822 August 2, 2024 15:40
@rfc2822 rfc2822 force-pushed the 52-make-clear-that-invalidpropertyexception-is-not-an-error branch from f6f905b to d38137b Compare August 3, 2024 11:45
Copy link
Member

@rfc2822 rfc2822 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, logging should be more intuitive now and we've on the go also fixed the root cause of #9.

@rfc2822 rfc2822 merged commit fe97db3 into main Aug 3, 2024
3 checks passed
@rfc2822 rfc2822 deleted the 52-make-clear-that-invalidpropertyexception-is-not-an-error branch August 3, 2024 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid InvalidPropertyException for valid properties
2 participants