Skip to content
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

Support all struct types for JSON encoding #2824

Merged
merged 1 commit into from
Feb 12, 2024

Conversation

quanturium
Copy link
Contributor

@quanturium quanturium commented Feb 10, 2024

The current implementation of KotlinConstructorBuilder.kt for constructor parameters order implemented here and here makes an inference that is incorrect: repeated fields and isMap fields are not the only proto types that are represented as a List or Map kotlin types. google.protobuf.Struct, google.protobuf.ListValue are also represented as List and Map kotlin types making the current mapping incorrect. This lead to the following exception:

Caused by: java.util.NoSuchElementException: ArrayDeque is empty.
	at kotlin.collections.ArrayDeque.removeFirst(ArrayDeque.kt:145)
	at com.squareup.wire.KotlinConstructorBuilder.build(KotlinConstructorBuilder.kt:114)

This PR fixes this by just removing the extra logic for parameter ordering since this commit fixed the issue by correctly ordering constructor parameters.

cc @dnkoutso

@quanturium quanturium changed the title [WIP] Support all struct types for Moshi json encoding Support all struct types for Moshi json encoding Feb 10, 2024
@quanturium quanturium marked this pull request as ready for review February 10, 2024 20:12
@dnkoutso
Copy link
Collaborator

Cc @oldergod for kotlin changes, I mostly focus on the SwiftGenerator.

as a nit maybe remove WIP from commit message?

@dnkoutso
Copy link
Collaborator

Thank you for the contribution!

@oldergod
Copy link
Member

Thank you!

Two things:

  • Could you sign our Contributor License Agreement so that I can eventually merge it?
  • Could you push again once you've run ./gradlew generateTests ?

@oldergod oldergod changed the title Support all struct types for Moshi json encoding Support all struct types for JSON encoding Feb 11, 2024
@quanturium
Copy link
Contributor Author

Addressed feedback above

| "struct": {"key1":"value1"}
|}
""".trimMargin()
assertJsonEquals(gson.toJson(value), json)
Copy link
Collaborator

Choose a reason for hiding this comment

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

does this test fail without your change? Just verifying!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test wasn't properly testing out the constructor creation which I just fixed. To your question, yes it does. Here is the issue:

java.util.NoSuchElementException: ArrayDeque is empty.
	at kotlin.collections.ArrayDeque.removeFirst(ArrayDeque.kt:145)
	at com.squareup.wire.KotlinConstructorBuilder.build(KotlinConstructorBuilder.kt:114)
	at com.squareup.wire.MessageTypeAdapter.read(MessageTypeAdapter.kt:77)
	at com.squareup.wire.MessageTypeAdapter.read(MessageTypeAdapter.kt:25)
	at com.google.gson.TypeAdapter$1.read(TypeAdapter.java:204)

^ This is due to the custom re-ordering we were doing pre this PR

@quanturium
Copy link
Contributor Author

quanturium commented Feb 12, 2024

I don't think the jvm17 check failure is related to my PR.

@oldergod
Copy link
Member

Thanks a lot

@oldergod oldergod merged commit 25edc5b into square:master Feb 12, 2024
7 checks passed
@quanturium
Copy link
Contributor Author

@oldergod Thanks a lot for merging this PR. What timeline shall we expect for the next version of wire including this?

@oldergod
Copy link
Member

It's on 4.9.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants