Skip to content

Commit ea36c22

Browse files
fix(C#): added dependency usings based on types present in renderContext (#2606)
* added dependecy usings based on types present in renderContext * PR change : removed array usage for emiting , added local function to ensure single addition of "System.Collections.Generic" --------- Co-authored-by: inferrinizzard <[email protected]>
1 parent 4a20324 commit ea36c22

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
} from "../../Type";
2525
import {
2626
directlyReachableSingleNamedType,
27+
matchCompoundType,
2728
matchType,
2829
nullableFromUnion,
2930
removeNullFromUnion,
@@ -526,4 +527,25 @@ export class CSharpRenderer extends ConvenienceRenderer {
526527

527528
this.emitDefaultFollowingComments();
528529
}
530+
531+
protected emitDependencyUsings(): void {
532+
let genericEmited: boolean = false;
533+
let ensureGenericOnce = () => {
534+
if (!genericEmited) {
535+
this.emitUsing("System.Collections.Generic");
536+
genericEmited = true;
537+
}
538+
}
539+
this.typeGraph.allTypesUnordered().forEach(_ => {
540+
matchCompoundType(
541+
_,
542+
_arrayType => this._csOptions.useList ? ensureGenericOnce() : undefined,
543+
_classType => { },
544+
_mapType => ensureGenericOnce(),
545+
_objectType => { },
546+
_unionType => { }
547+
)
548+
});
549+
}
550+
529551
}

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

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

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

156158
super.emitUsings();
157159
this.ensureBlankLine();

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

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

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

158160
super.emitUsings();
159161
this.ensureBlankLine();

0 commit comments

Comments
 (0)