RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.8k stars 1.3k forks source link

Generate the DTO's in separate file #1009

Open andrewinkler1 opened 7 years ago

andrewinkler1 commented 7 years ago

Is it possible to generate the dto's outside the service methods in a separate file?

RicoSuter commented 7 years ago

Currently only for C#,

Duplicate: https://github.com/RSuter/NJsonSchema/issues/514

RicoSuter commented 7 years ago

You could however exclude all DTOs from generation (ExcludedTypeNames) generate, then exclude all clients generate again...

RicoSuter commented 7 years ago

Duplicate: https://github.com/RSuter/NSwag/issues/294

statler commented 6 years ago

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);
            }
        }
    }
}

`

schuettecarsten commented 6 years ago

Any news on this?

jeremy-collette commented 5 years ago

@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.

RicoSuter commented 5 years ago

This option is not yet available

IvanJosipovic commented 5 years ago

+1 needing this.

joni1700 commented 5 years ago

+1

kimlundjohansen commented 5 years ago

+1 need this

Bandwagoner commented 4 years ago

+1

RicoSuter commented 4 years ago

For C# output you can already specify a clients and separate contracts file output.

milanjaros commented 4 years ago

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:...

Bandwagoner commented 4 years ago

Any plans to support separation of DTOs generated in Typescript?

milanjaros commented 4 years ago

@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.

lafritay commented 3 years ago

@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?

jeremyVignelles commented 3 years ago

@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

lafritay commented 3 years ago

@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...

jeremyVignelles commented 3 years ago

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