Skip to content

Commit 9c7467c

Browse files
committed
added dependecy usings based on types present in renderContext
1 parent ed1ffa4 commit 9c7467c

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

packages/quicktype-core/src/language/CSharp/CSharpRenderer.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { assert } from "../../support/Support";
1010
import { type TargetLanguage } from "../../TargetLanguage";
1111
import { followTargetType } from "../../Transformers";
1212
import { type ClassProperty, type ClassType, type EnumType, type Type, type UnionType } from "../../Type";
13-
import { directlyReachableSingleNamedType, matchType, nullableFromUnion, removeNullFromUnion } from "../../TypeUtils";
13+
import { directlyReachableSingleNamedType, matchCompoundType, matchType, nullableFromUnion, removeNullFromUnion } from "../../TypeUtils";
1414

1515
import { type cSharpOptions } from "./language";
1616
import {
@@ -387,4 +387,21 @@ export class CSharpRenderer extends ConvenienceRenderer {
387387

388388
this.emitDefaultFollowingComments();
389389
}
390+
391+
protected emitDependencyUsings(): void {
392+
let nameSpaceForTypes: string[] = [];
393+
this.typeGraph.allTypesUnordered().forEach(_ => {
394+
matchCompoundType(
395+
_,
396+
_arrayType => this._csOptions.useList ? nameSpaceForTypes.push("System.Collections.Generic") : undefined,
397+
_classType => { },
398+
_mapType => nameSpaceForTypes.push("System.Collections.Generic"),
399+
_objectType => { },
400+
_unionType => { }
401+
)
402+
});
403+
nameSpaceForTypes = nameSpaceForTypes.filter((val, ind) => nameSpaceForTypes.indexOf(val) === ind );
404+
nameSpaceForTypes.forEach(this.emitUsing.bind(this));
405+
}
406+
390407
}

packages/quicktype-core/src/language/CSharp/NewtonSoftCSharpRenderer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ export class NewtonsoftCSharpRenderer extends CSharpRenderer {
120120
}
121121

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

126128
super.emitUsings();
127129
this.ensureBlankLine();

packages/quicktype-core/src/language/CSharp/SystemTextJsonCSharpRenderer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ export class SystemTextJsonCSharpRenderer extends CSharpRenderer {
120120
}
121121

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

126128
super.emitUsings();
127129
this.ensureBlankLine();

0 commit comments

Comments
 (0)