Open andrewinkler1 opened 7 years ago
Currently only for C#,
You could however exclude all DTOs from generation (ExcludedTypeNames) generate, then exclude all clients generate again...
Duplicate: https://github.com/RSuter/NSwag/issues/294
Something like this will do the job using NJsonSchema.CodeGeneration.TypeScript. Note that you will need to change your ts template to deal with your imports
`
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using NSwag;
using NJsonSchema.CodeGeneration.TypeScript;
using NJsonSchema.CodeGeneration;
namespace TypescriptFileSplitter
{
public partial class getTs
{
public async void getTS(object sender, EventArgs e)
{
if (!Directory.Exists(beDest.Text))
{
MessageBox.Show("The target directory does not exist.");
return;
}
var schemaSwagger = await SwaggerDocument.FromUrlAsync("http://localhost:60000/swagger/v2/swagger.json");
TypeScriptGeneratorSettings tsGenSettings = new TypeScriptGeneratorSettings()
{
ConvertConstructorInterfaceData = true,
TypeStyle = TypeScriptTypeStyle.KnockoutClass,
MarkOptionalProperties = false,
PropertyNameGenerator = new myPropertyNameGenerator(),
TypeNameGenerator = new myTypeNameGenerator(),
TypeScriptVersion = 2.4m
};
var _resolver = new TypeScriptTypeResolver(tsGenSettings);
_resolver.RegisterSchemaDefinitions(schemaSwagger.Definitions);
var generatorJ = new TypeScriptGenerator(schemaSwagger, tsGenSettings, _resolver);
var typeDef = generatorJ.GenerateTypes();
foreach (CodeArtifact codeArtifact in typeDef.Artifacts)
{
File.WriteAllText(Path.Combine(beDest.Text, codeArtifact.TypeName + ".ts"), codeArtifact.Code);
}
}
}
}
`
Any news on this?
@RicoSuter which option do we enable to have the DTOs in a separate file for C# code generation? I just see an option for one output file. I'm on v12.3.1.
This option is not yet available
+1 needing this.
+1
+1 need this
+1
For C# output you can already specify a clients and separate contracts file output.
To generate contracts in separate file use /GenerateContractsOutput:true /ContractsOutput:path/to/Dtos.Generated.cs /ContractsNamespace:Namespace.To.Dtos.Generated
You could also skip generating client by /GenerateClientClasses:false
, so you can also ommit /output:...
Any plans to support separation of DTOs generated in Typescript?
@justin-c-wong, do you mean to have every class and interface in separate files? Is that what you really want for generated code?
Or do you mean separation of DTOs from Client? ... Which can be achieved by these arguments:
/output:path/to/Dtos.Generated.ts /namespace:Path.To.Dtos.Generated /GenerateContractsOutput:true /GenerateClientClasses:false
Note, there are not /ContractsOutput
and /ContractsNamespace
as in example abov.
@milanjaros I'm able to get the clients and types generated in separate files using the method you mention above. I'm not able to figure out how to get the custom types in the Clients file to reference the types in the Types file though. Any tips on how to make that happen?
@lafritay : Generate them in the same namespace? If you do it that way and use their class name, the compiler should be able to find them without using
@jeremyVignelles Sorry, I meant for typescript, not C#. Unless the namespacing feature was supposed to work for typescript as well? It doesn't seem to work for me...
Indeed, sorry. I don't know if you can add your custom imports in the generated file by configuration, but you could create your own file template and put your using before the original code
Is it possible to generate the dto's outside the service methods in a separate file?