Skip to content

Commit

Permalink
added dependecy usings based on types present in renderContext
Browse files Browse the repository at this point in the history
  • Loading branch information
sakets594 committed May 25, 2024
1 parent ed1ffa4 commit 9c7467c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
19 changes: 18 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,21 @@ export class CSharpRenderer extends ConvenienceRenderer {

this.emitDefaultFollowingComments();
}

protected emitDependencyUsings(): void {
let nameSpaceForTypes: string[] = [];
this.typeGraph.allTypesUnordered().forEach(_ => {
matchCompoundType(
_,
_arrayType => this._csOptions.useList ? nameSpaceForTypes.push("System.Collections.Generic") : undefined,
_classType => { },
_mapType => nameSpaceForTypes.push("System.Collections.Generic"),
_objectType => { },
_unionType => { }
)
});
nameSpaceForTypes = nameSpaceForTypes.filter((val, ind) => nameSpaceForTypes.indexOf(val) === ind );
nameSpaceForTypes.forEach(this.emitUsing.bind(this));
}

}
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

0 comments on commit 9c7467c

Please sign in to comment.