Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {

private readonly _needNamespaces: boolean;

private get needConverterClass(): boolean {
return (
this._needHelpers ||
(this._needAttributes && (this.haveNamedUnions || this.haveEnums))
);
}

public constructor(
targetLanguage: TargetLanguage,
renderContext: RenderContext,
Expand Down Expand Up @@ -160,12 +167,14 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {
super.emitUsings();
this.ensureBlankLine();

for (const ns of [
"System.Globalization",
"Newtonsoft.Json",
"Newtonsoft.Json.Converters",
]) {
this.emitUsing(ns);
if (this.needConverterClass) {
this.emitUsing("System.Globalization");
}

this.emitUsing("Newtonsoft.Json");

if (this.needConverterClass) {
this.emitUsing("Newtonsoft.Json.Converters");
}

if (this._options.dense) {
Expand Down Expand Up @@ -1177,10 +1186,7 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {
this.emitSerializeClass();
}

if (
this._needHelpers ||
(this._needAttributes && (this.haveNamedUnions || this.haveEnums))
) {
if (this.needConverterClass) {
this.ensureBlankLine();
this.emitConverterClass();
this.forEachTransformation("leading-and-interposing", (n, t) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ export class SystemTextJsonCSharpRenderer extends CSharpRenderer {

private readonly _needNamespaces: boolean;

private get needConverterClass(): boolean {
return (
this._needHelpers ||
(this._needAttributes && (this.haveNamedUnions || this.haveEnums))
);
}

public constructor(
targetLanguage: TargetLanguage,
renderContext: RenderContext,
Expand Down Expand Up @@ -165,11 +172,14 @@ export class SystemTextJsonCSharpRenderer extends CSharpRenderer {
for (const ns of [
"System.Text.Json",
"System.Text.Json.Serialization",
"System.Globalization",
]) {
this.emitUsing(ns);
}

if (this.needConverterClass) {
this.emitUsing("System.Globalization");
}

if (this._options.dense) {
this.emitUsing([
denseJsonPropertyName,
Expand Down Expand Up @@ -1299,10 +1309,7 @@ export class SystemTextJsonCSharpRenderer extends CSharpRenderer {
this.emitSerializeClass();
}

if (
this._needHelpers ||
(this._needAttributes && (this.haveNamedUnions || this.haveEnums))
) {
if (this.needConverterClass) {
this.ensureBlankLine();
this.emitConverterClass();
this.forEachTransformation("leading-and-interposing", (n, t) =>
Expand Down
61 changes: 61 additions & 0 deletions test/unit/csharp-superfluous-usings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { describe, expect, test } from "vitest";

import {
InputData,
type RendererOptions,
jsonInputForTargetLanguage,
quicktype,
} from "../../packages/quicktype-core/src/index.js";

async function renderCSharp(rendererOptions: RendererOptions): Promise<string> {
const jsonInput = jsonInputForTargetLanguage("csharp");
await jsonInput.addSource({
name: "Sample",
samples: ['{"name":"Alice","age":30,"active":true}'],
});

const inputData = new InputData();
inputData.addInput(jsonInput);
const result = await quicktype({
inputData,
lang: "csharp",
rendererOptions,
});
return result.lines.join("\n");
}

describe("C# using statements", () => {
test("NewtonSoft attributes-only omits converter usings when no converter is emitted", async () => {
const output = await renderCSharp({
framework: "NewtonSoft",
features: "attributes-only",
});

expect(output).toContain("using Newtonsoft.Json;");
expect(output).not.toContain("using System.Globalization;");
expect(output).not.toContain("using Newtonsoft.Json.Converters;");
});

test("SystemTextJson attributes-only omits globalization when no converter is emitted", async () => {
const output = await renderCSharp({
framework: "SystemTextJson",
features: "attributes-only",
});

expect(output).toContain("using System.Text.Json.Serialization;");
expect(output).not.toContain("using System.Globalization;");
});

test.each([
["NewtonSoft", "using Newtonsoft.Json.Converters;"],
["SystemTextJson", "using System.Globalization;"],
] as Array<
["NewtonSoft" | "SystemTextJson", string]
>)("%s complete output keeps converter usings", async (framework, expectedUsing) => {
const output = await renderCSharp({ framework });

expect(output).toContain("using System.Globalization;");
expect(output).toContain(expectedUsing);
expect(output).toContain("IsoDateTime");
});
});
Loading