Skip to content

Commit

Permalink
feat(csharp): XML encode contents of <summary> and <example><code> co…
Browse files Browse the repository at this point in the history
…de comments (#4785)

* fix(csharp): XML encode contents of <summary> and <example><code> code comments
  • Loading branch information
Swimburger authored Oct 29, 2024
1 parent 0a048a4 commit 1d8c0a4
Show file tree
Hide file tree
Showing 34 changed files with 149 additions and 103 deletions.
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
9 changes: 8 additions & 1 deletion generators/csharp/sdk/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
- version: 1.8.4
irVersion: 53
createdAt: "2024-10-28"
changelogEntry:
- type: fix
summary: Make sure summary and code examples in XML code comments are XML encoded.
- version: 1.8.3
createdAt: "2024-10-28"
changelogEntry:
Expand All @@ -13,12 +19,13 @@
irVersion: 53
- version: 1.8.1
irVersion: 53
createdAt: "2024-10-08"
createdAt: '2024-10-08'
changelogEntry:
- type: fix
summary: |
Fixes a bug where the `OauthTokenProvider.cs` was incorrectly referencing
the endpoint method, causing code to fail to compile.
irVersion: 53
- version: 1.8.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.

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

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

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

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

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

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

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

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

Loading

0 comments on commit 1d8c0a4

Please sign in to comment.