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

@Headers annotation produces unexpected newline in generated code by ksp plugin #752

Open
Robotxm opened this issue Dec 17, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@Robotxm
Copy link

Robotxm commented Dec 17, 2024

Ktorfit version

2.2.0

What happened and how can we reproduce this issue?

For this function in my network interface:

@GET("https://example.com")
@Headers("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0")
suspend fun example(): String

It is converted to code below by ksp plugin:

override suspend fun example(): String {
  val _ext: HttpRequestBuilder.() -> Unit = {
      method = HttpMethod.parse("GET")
      url{
      takeFrom("https://example.com")
      }
      headers{
      append("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,
          like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0")
      } 
      }
  val _typeData = TypeData.createTypeData(
  typeInfo = typeInfo<String>(),
  )
  return _helper.suspendRequest(_typeData,_ext)!!
}

The User-Agent field contains an unexpected newline, which results in a compilation error.

What did you expect to happen?

No newline will be generated.

Is there anything else we need to know about?

No response

@Robotxm Robotxm added the bug Something isn't working label Dec 17, 2024
@Foso
Copy link
Owner

Foso commented Dec 17, 2024

Thank you for the report!

@Thijsiez
Copy link

Thijsiez commented Jan 6, 2025

There is a pretty big chance this is a KotlinPoet side effect. I've seen it wrap like this with longer generated lines in my project :)

@AntsyLich
Copy link

This has actually been fixed upstream https://square.github.io/kotlinpoet/changelog/#version-200

KotlinPoet 1.x used to replace space characters with newline characters whenever a given line of code exceeded the length limit. This usually led to better code formatting, but could also lead to compilation errors in generated code. Non-breaking spaces could be marked by the · character, but the discoverability of this feature wasn’t great.

KotlinPoet 2.0 does not wrap spaces, even if the line of code they occur in exceeds the length limit. The newly introduced ♢ character can be used to mark spaces that are safe to wrap, which can improve code formatting. The · character has been preserved for compatibility, but its behavior is now equivalent to a regular space character.

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

No branches or pull requests

4 participants