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

[.Net][Bug]: AutoGen.SourceGenerator doesn't encode " in structural comments #2872

Closed
LittleLittleCloud opened this issue Jun 6, 2024 · 11 comments · Fixed by #3068
Closed
Assignees
Labels
bug Something isn't working

Comments

@LittleLittleCloud
Copy link
Collaborator

Describe the bug

When the content in structural comments contains ", the generated code won't get compiled because the " breaks the string.

Steps to reproduce

No response

Model Used

No response

Expected Behavior

No response

Screenshots and logs

No response

Additional Information

No response

@LittleLittleCloud LittleLittleCloud added the bug Something isn't working label Jun 6, 2024
@LittleLittleCloud LittleLittleCloud added this to the AutoGen.Net 0.0.15 milestone Jun 6, 2024
@prithvi2226
Copy link
Contributor

Hi @LittleLittleCloud! This task seems to be interesting! Can I work on this task?

@LittleLittleCloud
Copy link
Collaborator Author

@prithvi2226 that would be awesome!

@prithvi2226
Copy link
Contributor

@LittleLittleCloud If possible, could you please assign this task to me? However, if this action doesn't make a significant difference, I am fine with it. The main priority is to ensure the issue is resolved and the repository progresses smoothly.

@LittleLittleCloud
Copy link
Collaborator Author

@prithvi2226 Any update on this issue

@prithvi2226
Copy link
Contributor

@prithvi2226 Any update on this issue

Hey @LittleLittleCloud , I am still working on it, been remote for a while, I can give you an update by Tuesday!? If that's okay?

@LittleLittleCloud
Copy link
Collaborator Author

@prithvi2226 Do you need more time on this one? Also please let me know if you need help!

@prithvi2226
Copy link
Contributor

Hi @LittleLittleCloud! Thank you very much for checking up on me, and thank you very much for taking the time to help me out with this. I am very sorry from my side, for not giving an update, to solve this bug.

Please Correct me if I am wrong, I have decided to work on the file dotnet/src/AutoGen.SourceGenerator/DocumentCommentExtension.cs , and DocumentCommentExtension class contains methods for handling documentation comments, and it's where I am going to add the logic to escape double quotes in the comments. And, in the method 'GetParameterDescriptionFromDocumentationCommentTriviaSyntax' looks like it processes descriptions for parameters from documentation comments. I hope I am on the right track! Thank you very much!

@LittleLittleCloud
Copy link
Collaborator Author

@prithvi2226 Thanks for your response.

IMO you don't need to go that further into DocumentCommentExtension.cs, the quickest solution is to encode " properly in

Description = @"<#=parameter.Description#>",
. (" -> "")

For example, when the comment contains "

/// <summary>
/// I have " in the summary and the generated code is breaking!
/// </summary>
/// <param name="city"></param>
/// <returns></returns>
[Function]
public async Task<string> GetWeatherAsync(string city)
{
    return await Task.FromResult($"The weather in {city} is sunny.");
}

The generated code is
image

As you can see, " breaks the generated code because FunctionTemplate.tt encodes description using @"...." and didn't process the " in the comment description.

The quickest fix is to encode " in description by doubling it (""). This is how " is encoded as well in verbatim string.

e.g.
image

@prithvi2226
Copy link
Contributor

Hi @LittleLittleCloud ,

Thank you very much for your guidance. Based on your suggestion, I will implement the fix directly in the FunctionCallTemplate.tt file. Here are the steps I plan to take:

Planned Changes:

  1. Modify the FunctionCallTemplate.tt File:

Locating the line that assigns the description (around line 84):

Description = @"<#=parameter.Description#>",

  1. I will modify this line to properly encode double quotes by doubling them (""), like so:

Description = @"<#= parameter.Description.Replace("\"", "\"\"") #>",

This approach should correctly handle double quotes in comments and prevent the generated code from breaking.

Please let me know if this solution looks good to you, and I will proceed with making these changes.

Thank you once again for your assistance!

Best regards,
Prithvi

@LittleLittleCloud
Copy link
Collaborator Author

You nailed it!

@prithvi2226
Copy link
Contributor

Hi @LittleLittleCloud ! I have created a pull request, let me know what you think about it. Thank you very much for your guidance and support. I would love to connect with you on Github or Slack to continue supporting this project.

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 a pull request may close this issue.

2 participants