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

Issue: Unwanted Generation of i-collection.ts and i-enumerable.ts #201

Open
MehdiElMellali opened this issue May 26, 2024 · 0 comments
Open

Comments

@MehdiElMellali
Copy link

Description

When using TypeGen to generate TypeScript models from C# classes, the configuration and generation process is unexpectedly producing files such as i-collection.ts and i-enumerable.ts. These files are not required and contain invalid TypeScript code. The desired outcome is to generate only the models from the specified namespaces.

Environment

  • .NET Version: 8
  • TypeGen Version: 5.0.1

tgconfig.json Configuration

  "outputPath": "C:/backapi", // Output path for generated files, relative to the project folder
  "fileNameConverters": ["PascalCaseToKebabCaseConverter"], // Converter chain used for converting C# type names to TypeScript file names
  "projectOutputFolder": "bin", // The project’s output folder
  "typeNameConverters": [],
  "tabLength": 2,
  "singleQuotes": true,
  "clearOutputDirectory": true,
  "externalAssemblyPaths": ["./bin"],
  "createIndexFile": false,
  "enumStringInitializersConverters": ["PascalCaseToCamelCaseConverter"],
  "generationSpecs": ["ApiModelsGenerationSpec"],
  "customTypeMappings": { "Microsoft.AspNetCore.Http.IFormFile": "File" },
  "explicitPublicAccessor": true,
  "typeBlacklist": [
    "IEnumerable",
    "IEntityDto",
    "IHasExtraProperties",
    "ICollection",
    "KeyValuePair",
    "IDictionary",
    "IReadOnlyCollection",
    "IDeserializationCallback"
  ] // Types that should not be generated
}

ApiModelsGenerationSpec.cs

{
    public ApiModelsGenerationSpec()
    {
        GetType().Assembly
              .GetTypes()
              .Where(x => x.IsClass && !x.IsAbstract 
              && x.Namespace.StartsWith("Articles", StringComparison.InvariantCulture))
              .ToList()
              .ForEach(x => AddInterface(x));
    }
}

Output (Unwanted)

  • i-collection.ts
     * This is a TypeGen auto-generated file.
     * Any changes made to this file can be lost when this file is regenerated.
     */
    
    import { IEnumerable } from './i-enumerable';
    
    export interface T[] extends T[] {
      count: number;
      isReadOnly: boolean;
    }
  • i-enumerable.ts
     * This is a TypeGen auto-generated file.
     * Any changes made to this file can be lost when this file is regenerated.
     */
    
    export interface T[] {
    
    }

Expected Output

Only the models from the specified namespaces should be generated into TypeScript files, without generating any interfaces for collections like IEnumerable or ICollection.

Steps to Reproduce

  1. Set up a .NET 8 project.

  2. Add TypeGen 5.0.1 as a package.

  3. Configure tgconfig.json as shown above.

  4. Implement ApiModelsGenerationSpec as shown above.

  5. Run the TypeGen generation command:

    typegen generate -p "path/to/project" -c "path/to/tgconfig.json"

Proposed Solution

  1. Ensure IEnumerable and ICollection are blacklisted correctly in tgconfig.json.
  2. Modify ApiModelsGenerationSpec to avoid processing any types related to collections.

Additional Notes

  • The issue might be related to default behavior or misconfiguration.
  • Custom mappings and blacklists should theoretically prevent these files from being generated.

Request

Please advise on how to configure TypeGen properly to avoid generating unnecessary files like i-collection.ts and i-enumerable.ts. Any guidance on correcting the configuration or updating the TypeGen tool itself would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant