Skip to content

Commit

Permalink
fix: Fixed schema generation using NSwag.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Dec 4, 2023
1 parent f4c90f9 commit c5b0030
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 4,463 deletions.
41 changes: 37 additions & 4 deletions src/libs/OpenApiGenerator/OpenApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using NSwag;
using NSwag.CodeGeneration.CSharp;
using NSwag.CodeGeneration.OperationNameGenerators;
using OpenApiSchema = Microsoft.OpenApi.Models.OpenApiSchema;

namespace H.Generators;

Expand Down Expand Up @@ -38,13 +39,20 @@ private static EquatableArray<FileWithName> GetSourceCode(
var openApi = Task.Run(() =>
OpenApiYamlDocument.FromYamlAsync(yaml, cancellationToken), cancellationToken).Result;
var openApiDocument = new OpenApiStringReader().Read(yaml, out _);
var schemaKeys = openApiDocument.Components?.Schemas?.Keys ?? Array.Empty<string>();
var schemas = openApiDocument.Components?.Schemas ?? new Dictionary<string, OpenApiSchema>();
var prefix = Path.GetFileName(text.Path);

var allAdditionalExcludedTypeNames = schemas.Keys
.Select(schemaKey => GetAdditionalExcludedTypeNames(schemaKey, schemas))
.SelectMany(x => x)
.ToArray();

var files = new List<FileWithName>();
files.AddRange(schemaKeys.Select(schemaKey =>
files.AddRange(schemas.Keys.Select(schemaKey =>
{
var excludedTypeNames = schemaKeys.Where(x => x != schemaKey).ToArray();
var excludedTypeNames = schemas.Keys
.Where(x => x != schemaKey)
.Concat(allAdditionalExcludedTypeNames.Except(GetAdditionalExcludedTypeNames(schemaKey, schemas)))
.ToArray();

return new FileWithName(
Name: $"{prefix}.Models.{schemaKey}.cs",
Expand All @@ -54,6 +62,31 @@ private static EquatableArray<FileWithName> GetSourceCode(
return files.ToImmutableArray();
}

private static string[] GetAdditionalExcludedTypeNames(
string key,
IDictionary<string, OpenApiSchema> schemas)
{
var schema = schemas[key];
var existingNames = new HashSet<string>(
schemas.Keys.Select(x => x.ToPropertyName()).ToArray());
var enums = schema.Properties
.Where(x => x.Value.Enum != null && x.Value.Enum.Any())
.Select(x => $"{key}{x.Key.ToPropertyName()}")
.ToArray();
var objects = schema.Properties
.Where(static x => x.Value.Type == "object" && x.Value.Reference == null)
.SelectMany(x => existingNames.Contains(x.Key.ToPropertyName())
? new[]
{
$"{x.Key.ToPropertyName()}1",
} : Array.Empty<string>())
.ToArray();

return enums
.Concat(objects)
.ToArray();
}

private static string Generate(
OpenApiDocument openApi,
string[]? excludedTypeNames = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,69 +69,6 @@ public System.Collections.Generic.IDictionary<string, object> AdditionalProperti

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum AsnResponseType
{

[System.Runtime.Serialization.EnumMember(Value = @"isp")]
Isp = 0,

[System.Runtime.Serialization.EnumMember(Value = @"business")]
Business = 1,

[System.Runtime.Serialization.EnumMember(Value = @"education")]
Education = 2,

[System.Runtime.Serialization.EnumMember(Value = @"hosting")]
Hosting = 3,

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum CompanyType
{

[System.Runtime.Serialization.EnumMember(Value = @"isp")]
Isp = 0,

[System.Runtime.Serialization.EnumMember(Value = @"business")]
Business = 1,

[System.Runtime.Serialization.EnumMember(Value = @"education")]
Education = 2,

[System.Runtime.Serialization.EnumMember(Value = @"hosting")]
Hosting = 3,

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Error1
{

[System.Text.Json.Serialization.JsonPropertyName("title")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string Title { get; set; } = default!;

[System.Text.Json.Serialization.JsonPropertyName("message")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string Message { get; set; } = default!;

private System.Collections.Generic.IDictionary<string, object>? _additionalProperties;

[System.Text.Json.Serialization.JsonExtensionData]
public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
{
get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, object>()); }
private set { _additionalProperties = value; }
}

}


}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,51 +132,6 @@ public enum AsnResponseType

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum CompanyType
{

[System.Runtime.Serialization.EnumMember(Value = @"isp")]
Isp = 0,

[System.Runtime.Serialization.EnumMember(Value = @"business")]
Business = 1,

[System.Runtime.Serialization.EnumMember(Value = @"education")]
Education = 2,

[System.Runtime.Serialization.EnumMember(Value = @"hosting")]
Hosting = 3,

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Error1
{

[System.Text.Json.Serialization.JsonPropertyName("title")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string Title { get; set; } = default!;

[System.Text.Json.Serialization.JsonPropertyName("message")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string Message { get; set; } = default!;

private System.Collections.Generic.IDictionary<string, object>? _additionalProperties;

[System.Text.Json.Serialization.JsonExtensionData]
public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
{
get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, object>()); }
private set { _additionalProperties = value; }
}

}


}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,69 +57,6 @@ public System.Collections.Generic.IDictionary<string, object> AdditionalProperti

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum AsnResponseType
{

[System.Runtime.Serialization.EnumMember(Value = @"isp")]
Isp = 0,

[System.Runtime.Serialization.EnumMember(Value = @"business")]
Business = 1,

[System.Runtime.Serialization.EnumMember(Value = @"education")]
Education = 2,

[System.Runtime.Serialization.EnumMember(Value = @"hosting")]
Hosting = 3,

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum CompanyType
{

[System.Runtime.Serialization.EnumMember(Value = @"isp")]
Isp = 0,

[System.Runtime.Serialization.EnumMember(Value = @"business")]
Business = 1,

[System.Runtime.Serialization.EnumMember(Value = @"education")]
Education = 2,

[System.Runtime.Serialization.EnumMember(Value = @"hosting")]
Hosting = 3,

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Error1
{

[System.Text.Json.Serialization.JsonPropertyName("title")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string Title { get; set; } = default!;

[System.Text.Json.Serialization.JsonPropertyName("message")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string Message { get; set; } = default!;

private System.Collections.Generic.IDictionary<string, object>? _additionalProperties;

[System.Text.Json.Serialization.JsonExtensionData]
public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
{
get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, object>()); }
private set { _additionalProperties = value; }
}

}


}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,6 @@ public System.Collections.Generic.IDictionary<string, object> AdditionalProperti

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum AsnResponseType
{

[System.Runtime.Serialization.EnumMember(Value = @"isp")]
Isp = 0,

[System.Runtime.Serialization.EnumMember(Value = @"business")]
Business = 1,

[System.Runtime.Serialization.EnumMember(Value = @"education")]
Education = 2,

[System.Runtime.Serialization.EnumMember(Value = @"hosting")]
Hosting = 3,

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum CompanyType
{
Expand All @@ -94,33 +76,6 @@ public enum CompanyType

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Error1
{

[System.Text.Json.Serialization.JsonPropertyName("title")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string Title { get; set; } = default!;

[System.Text.Json.Serialization.JsonPropertyName("message")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string Message { get; set; } = default!;

private System.Collections.Generic.IDictionary<string, object>? _additionalProperties;

[System.Text.Json.Serialization.JsonExtensionData]
public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
{
get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, object>()); }
private set { _additionalProperties = value; }
}

}


}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,69 +59,6 @@ public System.Collections.Generic.IDictionary<string, object> AdditionalProperti

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum AsnResponseType
{

[System.Runtime.Serialization.EnumMember(Value = @"isp")]
Isp = 0,

[System.Runtime.Serialization.EnumMember(Value = @"business")]
Business = 1,

[System.Runtime.Serialization.EnumMember(Value = @"education")]
Education = 2,

[System.Runtime.Serialization.EnumMember(Value = @"hosting")]
Hosting = 3,

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0))")]
public enum CompanyType
{

[System.Runtime.Serialization.EnumMember(Value = @"isp")]
Isp = 0,

[System.Runtime.Serialization.EnumMember(Value = @"business")]
Business = 1,

[System.Runtime.Serialization.EnumMember(Value = @"education")]
Education = 2,

[System.Runtime.Serialization.EnumMember(Value = @"hosting")]
Hosting = 3,

}

[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0))")]
public partial class Error1
{

[System.Text.Json.Serialization.JsonPropertyName("title")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string Title { get; set; } = default!;

[System.Text.Json.Serialization.JsonPropertyName("message")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string Message { get; set; } = default!;

private System.Collections.Generic.IDictionary<string, object>? _additionalProperties;

[System.Text.Json.Serialization.JsonExtensionData]
public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
{
get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, object>()); }
private set { _additionalProperties = value; }
}

}


}

Expand Down
Loading

0 comments on commit c5b0030

Please sign in to comment.