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

feat(csharp): XML encode contents of <summary> and <example><code> code comments #4785

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fern/pages/changelogs/csharp-model/2024-10-01.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.0.2
**`(fix):`** Make sure summary and code examples in XML code comments are XML encoded.

3 changes: 3 additions & 0 deletions fern/pages/changelogs/csharp-sdk/2024-10-01.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.7.1
**`(fix):`** Make sure summary and code examples in XML code comments are XML encoded.

8 changes: 3 additions & 5 deletions generators/csharp/codegen/src/ast/Class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ClassInstantiation } from "./ClassInstantiation";
import { ClassReference } from "./ClassReference";
import { CodeBlock } from "./CodeBlock";
import { AstNode } from "./core/AstNode";
import { DocXmlWriter } from "./core/DocXmlWriter";
import { Writer } from "./core/Writer";
import { Field } from "./Field";
import { Interface } from "./Interface";
Expand Down Expand Up @@ -168,11 +169,8 @@ export class Class extends AstNode {
}

if (this.summary != null) {
writer.writeLine("/// <summary>");
this.summary.split("\n").forEach((line) => {
writer.writeLine(`/// ${line}`);
});
writer.writeLine("/// </summary>");
const docXmlWriter = new DocXmlWriter(writer);
docXmlWriter.writeNodeWithEscaping("summary", this.summary);
}
if (this.annotations.length > 0) {
writer.write("[");
Expand Down
8 changes: 3 additions & 5 deletions generators/csharp/codegen/src/ast/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Annotation } from "./Annotation";
import { ClassReference } from "./ClassReference";
import { CodeBlock } from "./CodeBlock";
import { AstNode } from "./core/AstNode";
import { DocXmlWriter } from "./core/DocXmlWriter";
import { Writer } from "./core/Writer";
import { Type } from "./Type";

Expand Down Expand Up @@ -108,11 +109,8 @@ export class Field extends AstNode {

public write(writer: Writer): void {
if (this.summary != null) {
writer.writeLine("/// <summary>");
this.summary.split("\n").forEach((line) => {
writer.writeLine(`/// ${line}`);
});
writer.writeLine("/// </summary>");
const docXmlWriter = new DocXmlWriter(writer);
docXmlWriter.writeNodeWithEscaping("summary", this.summary);
}

if (this.annotations.length > 0) {
Expand Down
23 changes: 8 additions & 15 deletions generators/csharp/codegen/src/ast/Method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Annotation } from "./Annotation";
import { ClassReference } from "./ClassReference";
import { CodeBlock } from "./CodeBlock";
import { AstNode } from "./core/AstNode";
import { DocXmlWriter } from "./core/DocXmlWriter";
import { Writer } from "./core/Writer";
import { Parameter } from "./Parameter";
import { Type } from "./Type";
Expand Down Expand Up @@ -89,24 +90,16 @@ export class Method extends AstNode {
}

public write(writer: Writer): void {
const docXmlWriter = new DocXmlWriter(writer);
if (this.summary != null) {
writer.writeLine("/// <summary>");
this.summary.split("\n").forEach((line) => {
writer.writeLine(`/// ${line}`);
});

writer.writeLine("/// </summary>");
docXmlWriter.writeNodeWithEscaping("summary", this.summary);
}
if (this.codeExample != null) {
writer.writeLine("/// <example>");
writer.writeLine("/// <code>");
this.codeExample.split("\n").forEach((line) => {
if (line !== "") {
writer.writeLine(`/// ${line}`);
}
});
writer.writeLine("/// </code>");
writer.writeLine("/// </example>");
docXmlWriter.writeOpenNode("example");
docXmlWriter.writeOpenNode("code");
docXmlWriter.writeMultilineWithEscaping(this.codeExample);
docXmlWriter.writeCloseNode("code");
docXmlWriter.writeCloseNode("example");
}

if (this.annotations.length > 0) {
Expand Down
54 changes: 54 additions & 0 deletions generators/csharp/codegen/src/ast/core/DocXmlWriter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Writer } from "..";

export class DocXmlWriter {
writer: Writer;
constructor(writer: Writer) {
this.writer = writer;
}

public writeLine(text?: string): void {
this.writer.writeLine(`/// ${text}`);
}

public writePrefix(): this {
this.writer.write("/// ");
return this;
}

public writeOpenNode(nodeName: string): void {
this.writeLine(`<${nodeName}>`);
}

public writeCloseNode(nodeName: string): void {
this.writeLine(`</${nodeName}>`);
}

public writeNode(nodeName: string, text: string): void {
this.writeOpenNode(nodeName);
this.writeMultiline(text);
this.writeCloseNode(nodeName);
}

public writeNodeWithEscaping(nodeName: string, text: string): void {
this.writeOpenNode(nodeName);
this.writeMultilineWithEscaping(text);
this.writeCloseNode(nodeName);
}

public writeMultiline(text: string): void {
text.trim()
.split("\n")
.forEach((line) => {
this.writeLine(line);
});
}

public writeMultilineWithEscaping(text: string): void {
text = this.escapeXmlDocContent(text);
this.writeMultiline(text);
}

private escapeXmlDocContent(text: string): string {
return text.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
}
}
3 changes: 2 additions & 1 deletion generators/csharp/codegen/src/ast/core/Writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ClassReference } from "..";
import { csharp } from "../..";
import { BaseCsharpCustomConfigSchema } from "../../custom-config";
import { AstNode } from "./AstNode";
import { DocXmlWriter } from "./DocXmlWriter";

type Alias = string;
type Namespace = string;
Expand Down Expand Up @@ -100,7 +101,7 @@ export class Writer extends AbstractWriter {
if (imports.length > 0) {
return `${imports}
#nullable enable

${this.buffer}`;
}
}
Expand Down
6 changes: 6 additions & 0 deletions generators/csharp/model/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
- version: 0.0.2
createdAt: '2024-10-01'
changelogEntry:
- type: fix
summary: Make sure summary and code examples in XML code comments are XML encoded.
irVersion: 33
- version: 0.0.1
createdAt: '2024-03-31'
changelogEntry:
Expand Down
6 changes: 6 additions & 0 deletions generators/csharp/sdk/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
- version: 1.7.1
createdAt: '2024-10-01'
changelogEntry:
- type: fix
summary: Make sure summary and code examples in XML code comments are XML encoded.
irVersion: 53
- version: 1.7.0
createdAt: '2024-08-29'
changelogEntry:
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading