-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
SwaggerToCSharpGeneratorBase.cs
82 lines (71 loc) · 3.98 KB
/
SwaggerToCSharpGeneratorBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//-----------------------------------------------------------------------
// <copyright file="SwaggerToCSharpGeneratorBase.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
using NJsonSchema;
using NJsonSchema.CodeGeneration.CSharp;
using NSwag.CodeGeneration.CSharp.Models;
namespace NSwag.CodeGeneration.CSharp
{
/// <summary>The CSharp generator base class.</summary>
public abstract class SwaggerToCSharpGeneratorBase : ClientGeneratorBase<CSharpOperationModel, CSharpParameterModel, CSharpResponseModel>
{
private readonly SwaggerToCSharpGeneratorSettings _settings;
private readonly SwaggerDocument _document;
/// <summary>Initializes a new instance of the <see cref="SwaggerToCSharpGeneratorBase"/> class.</summary>
/// <param name="document">The document.</param>
/// <param name="settings">The settings.</param>
/// <param name="resolver">The resolver.</param>
protected SwaggerToCSharpGeneratorBase(SwaggerDocument document, SwaggerToCSharpGeneratorSettings settings, CSharpTypeResolver resolver)
: base(document, settings.CodeGeneratorSettings, resolver)
{
_document = document;
_settings = settings;
}
/// <summary>Generates the file.</summary>
/// <param name="clientCode">The client code.</param>
/// <param name="clientClasses">The client classes.</param>
/// <param name="outputType">Type of the output.</param>
/// <returns>The code.</returns>
protected override string GenerateFile(string clientCode, IEnumerable<string> clientClasses, ClientGeneratorOutputType outputType)
{
var model = new CSharpFileTemplateModel(clientCode, outputType, _document, _settings, this, (CSharpTypeResolver)Resolver);
var template = _settings.CodeGeneratorSettings.TemplateFactory.CreateTemplate("CSharp", "File", model);
return template.Render();
}
/// <summary>Gets the type.</summary>
/// <param name="schema">The schema.</param>
/// <param name="isNullable">if set to <c>true</c> [is nullable].</param>
/// <param name="typeNameHint">The type name hint.</param>
/// <returns>The type name.</returns>
public override string GetTypeName(JsonSchema4 schema, bool isNullable, string typeNameHint)
{
if (schema == null)
return "void";
if (schema.ActualSchema.Type == JsonObjectType.File)
return "FileResponse";
if (schema.ActualSchema.IsAnyType)
return "object";
return Resolver.Resolve(schema.ActualSchema, isNullable, typeNameHint)
.Replace(_settings.CSharpGeneratorSettings.ArrayType + "<", _settings.ResponseArrayType + "<")
.Replace(_settings.CSharpGeneratorSettings.DictionaryType + "<", _settings.ResponseDictionaryType + "<");
}
/// <summary>Creates a new resolver, adds the given schema definitions and registers an exception schema if available.</summary>
/// <param name="settings">The settings.</param>
/// <param name="document">The document </param>
public static CSharpTypeResolver CreateResolverWithExceptionSchema(CSharpGeneratorSettings settings, SwaggerDocument document)
{
var exceptionSchema = document.Definitions.ContainsKey("Exception") ? document.Definitions["Exception"] : null;
var resolver = new CSharpTypeResolver(settings, exceptionSchema);
resolver.RegisterSchemaDefinitions(document.Definitions
.Where(p => p.Value != exceptionSchema)
.ToDictionary(p => p.Key, p => p.Value));
return resolver;
}
}
}