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

fix(C#): added dependency usings based on types present in renderContext #2606

Open
wants to merge 2 commits into
base: master
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
23 changes: 22 additions & 1 deletion packages/quicktype-core/src/language/CSharp/CSharpRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { assert } from "../../support/Support";
import { type TargetLanguage } from "../../TargetLanguage";
import { followTargetType } from "../../Transformers";
import { type ClassProperty, type ClassType, type EnumType, type Type, type UnionType } from "../../Type";
import { directlyReachableSingleNamedType, matchType, nullableFromUnion, removeNullFromUnion } from "../../TypeUtils";
import { directlyReachableSingleNamedType, matchCompoundType, matchType, nullableFromUnion, removeNullFromUnion } from "../../TypeUtils";

import { type cSharpOptions } from "./language";
import {
Expand Down Expand Up @@ -387,4 +387,25 @@ export class CSharpRenderer extends ConvenienceRenderer {

this.emitDefaultFollowingComments();
}

protected emitDependencyUsings(): void {
let genericEmited: boolean = false;
let ensureGenericOnce = () => {
if (!genericEmited) {
this.emitUsing("System.Collections.Generic");
genericEmited = true;
}
}
this.typeGraph.allTypesUnordered().forEach(_ => {
matchCompoundType(
_,
_arrayType => this._csOptions.useList ? ensureGenericOnce() : undefined,
_classType => { },
_mapType => ensureGenericOnce(),
_objectType => { },
_unionType => { }
)
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {
}

protected emitUsings(): void {
// FIXME: We need System.Collections.Generic whenever we have maps or use List.
if (!this._needAttributes && !this._needHelpers) return;
if (!this._needAttributes && !this._needHelpers) {
this.emitDependencyUsings();
return;
}

super.emitUsings();
this.ensureBlankLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ export class SystemTextJsonCSharpRenderer extends CSharpRenderer {
}

protected emitUsings(): void {
// FIXME: We need System.Collections.Generic whenever we have maps or use List.
if (!this._needAttributes && !this._needHelpers) return;
if (!this._needAttributes && !this._needHelpers) {
this.emitDependencyUsings();
return;
}

super.emitUsings();
this.ensureBlankLine();
Expand Down
Loading