Skip to content

Commit

Permalink
fix: Fixed bug with nullable enums.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Jun 5, 2024
1 parent e105a70 commit 3e3f1f5
Show file tree
Hide file tree
Showing 301 changed files with 16,835 additions and 22 deletions.
20 changes: 19 additions & 1 deletion src/libs/OpenApiGenerator.Core/Generation/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ .. includedTags.Select(x => PropertyData.Default with
{
Converters = models
.Where(x => x.Style == ModelStyle.Enumeration && x.JsonSerializerType != JsonSerializerType.NewtonsoftJson)
.Select(x => $"global::OpenApiGenerator.JsonConverters.{x.ClassName}JsonConverter")
.SelectMany(x => new [] {
$"global::OpenApiGenerator.JsonConverters.{x.ClassName}JsonConverter",
$"global::OpenApiGenerator.JsonConverters.{x.ClassName}NullableJsonConverter"
})
.ToImmutableArray(),
};
}
Expand Down Expand Up @@ -323,6 +326,21 @@ public static FileWithName GetSourceCodeForEnumJsonConverter(
Name: $"JsonConverters.{data.ClassName}.g.cs",
Text: Sources.GenerateEnumJsonConverter(data, cancellationToken: cancellationToken));
}

public static FileWithName GetSourceCodeForEnumNullableJsonConverter(
ModelData data,
CancellationToken cancellationToken = default)
{
if (data.Style != ModelStyle.Enumeration ||
data.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
{
return FileWithName.Empty;
}

return new FileWithName(
Name: $"JsonConverters.{data.ClassName}Nullable.g.cs",
Text: Sources.GenerateEnumNullableJsonConverter(data, cancellationToken: cancellationToken));
}

public static FileWithName GetSourceCodeForAnyOfJsonConverterFactory(
AnyOfData anyOf,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,74 @@ public override void Write(
}}
}}
}}
";
}

public static string GenerateEnumNullableJsonConverter(
ModelData model,
CancellationToken cancellationToken = default)
{
if (model.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
{
return string.Empty;
}

return $@"#nullable enable
namespace OpenApiGenerator.JsonConverters
{{
/// <inheritdoc />
public sealed class {model.ClassName}NullableJsonConverter : global::System.Text.Json.Serialization.JsonConverter<{model.GlobalClassName}?>
{{
/// <inheritdoc />
public override {model.GlobalClassName}? Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
{{
switch (reader.TokenType)
{{
case global::System.Text.Json.JsonTokenType.String:
{{
var stringValue = reader.GetString();
if (stringValue != null)
{{
return {model.GlobalClassName}Extensions.ToEnum(stringValue);
}}
break;
}}
case global::System.Text.Json.JsonTokenType.Number:
{{
var numValue = reader.GetInt32();
return ({model.GlobalClassName})numValue;
}}
default:
throw new global::System.ArgumentOutOfRangeException(nameof(reader));
}}
return default;
}}
/// <inheritdoc />
public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
{model.GlobalClassName}? value,
global::System.Text.Json.JsonSerializerOptions options)
{{
writer = writer ?? throw new global::System.ArgumentNullException(nameof(writer));
if (value == null)
{{
writer.WriteNullValue();
}}
else
{{
writer.WriteStringValue({model.GlobalClassName}Extensions.ToValueString(value.Value));
}}
}}
}}
}}
";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace OpenApiGenerator.JsonConverters;

public sealed class TestEnumNullableJsonStringEnumConverter : global::System.Text.Json.Serialization.JsonConverter<TestEnum?>
{
public override TestEnum? Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case global::System.Text.Json.JsonTokenType.String:
{
var stringValue = reader.GetString();
if (stringValue != null)
{
return TestEnumExtensions.ToEnum(stringValue);
}

break;
}
case global::System.Text.Json.JsonTokenType.Number:
{
return (TestEnum)reader.GetInt32();
}
default:
throw new global::System.ArgumentOutOfRangeException(nameof(reader));
}

return default;
}

public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
TestEnum? value,
global::System.Text.Json.JsonSerializerOptions options)
{
writer = writer ?? throw new global::System.ArgumentNullException(nameof(writer));

if (value == null)
{
writer.WriteNullValue();
}
else
{
writer.WriteStringValue(value.Value.ToValueString());
}
}
}
15 changes: 15 additions & 0 deletions src/libs/OpenApiGenerator/SdkGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.SelectMany(static (x, _) => x.Models)
.SelectAndReportExceptions(GetEnumJsonConverterSourceCode, context, Id)
.AddSource(context);
data
.SelectMany(static (x, _) => x.Models)
.SelectAndReportExceptions(GetEnumNullableJsonConverterSourceCode, context, Id)
.AddSource(context);

data
.SelectMany(static (x, _) => x.AnyOfs)
Expand Down Expand Up @@ -122,6 +126,17 @@ private static FileWithName GetEnumJsonConverterSourceCode(
Text: fileWithName.Text);
}

private static FileWithName GetEnumNullableJsonConverterSourceCode(
ModelData model,
CancellationToken cancellationToken = default)
{
var fileWithName = Data.GetSourceCodeForEnumNullableJsonConverter(model, cancellationToken);

return new FileWithName(
Name: fileWithName.Name,
Text: fileWithName.Text);
}

private static FileWithName GetAnyOfJsonConverterFactorySourceCode(
AnyOfData anyOf,
CancellationToken cancellationToken = default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,45 @@ public Api(
Converters =
{
new global::OpenApiGenerator.JsonConverters.RepositorySquashMergeCommitTitleJsonConverter(),
new global::OpenApiGenerator.JsonConverters.RepositorySquashMergeCommitTitleNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.RepositorySquashMergeCommitMessageJsonConverter(),
new global::OpenApiGenerator.JsonConverters.RepositorySquashMergeCommitMessageNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.RepositoryMergeCommitTitleJsonConverter(),
new global::OpenApiGenerator.JsonConverters.RepositoryMergeCommitTitleNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.RepositoryMergeCommitMessageJsonConverter(),
new global::OpenApiGenerator.JsonConverters.RepositoryMergeCommitMessageNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.SecurityAndAnalysisAdvancedSecurityStatusJsonConverter(),
new global::OpenApiGenerator.JsonConverters.SecurityAndAnalysisAdvancedSecurityStatusNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.SecurityAndAnalysisDependabotSecurityUpdatesStatusJsonConverter(),
new global::OpenApiGenerator.JsonConverters.SecurityAndAnalysisDependabotSecurityUpdatesStatusNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.SecurityAndAnalysisSecretScanningStatusJsonConverter(),
new global::OpenApiGenerator.JsonConverters.SecurityAndAnalysisSecretScanningStatusNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.SecurityAndAnalysisSecretScanningPushProtectionStatusJsonConverter(),
new global::OpenApiGenerator.JsonConverters.SecurityAndAnalysisSecretScanningPushProtectionStatusNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.ImportStatusJsonConverter(),
new global::OpenApiGenerator.JsonConverters.ImportStatusNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.PagesHttpsCertificateStateJsonConverter(),
new global::OpenApiGenerator.JsonConverters.PagesHttpsCertificateStateNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.PageStatusJsonConverter(),
new global::OpenApiGenerator.JsonConverters.PageStatusNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.PageProtectedDomainStateJsonConverter(),
new global::OpenApiGenerator.JsonConverters.PageProtectedDomainStateNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.PageBuildTypeJsonConverter(),
new global::OpenApiGenerator.JsonConverters.PageBuildTypeNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.MigrationsListForOrgExcludeJsonConverter(),
new global::OpenApiGenerator.JsonConverters.MigrationsListForOrgExcludeNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.MigrationsStartForOrgRequestExcludeJsonConverter(),
new global::OpenApiGenerator.JsonConverters.MigrationsStartForOrgRequestExcludeNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.MigrationsGetStatusForOrgExcludeJsonConverter(),
new global::OpenApiGenerator.JsonConverters.MigrationsGetStatusForOrgExcludeNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.MigrationsStartImportRequestVcsJsonConverter(),
new global::OpenApiGenerator.JsonConverters.MigrationsStartImportRequestVcsNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.MigrationsUpdateImportRequestVcsJsonConverter(),
new global::OpenApiGenerator.JsonConverters.MigrationsUpdateImportRequestVcsNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.MigrationsSetLfsPreferenceRequestUseLfsJsonConverter(),
new global::OpenApiGenerator.JsonConverters.MigrationsSetLfsPreferenceRequestUseLfsNullableJsonConverter(),
new global::OpenApiGenerator.JsonConverters.MigrationsStartForAuthenticatedUserRequestExcludeJsonConverter(),
new global::OpenApiGenerator.JsonConverters.MigrationsStartForAuthenticatedUserRequestExcludeNullableJsonConverter(),
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//HintName: JsonConverters.ImportStatusNullable.g.cs
#nullable enable

namespace OpenApiGenerator.JsonConverters
{
/// <inheritdoc />
public sealed class ImportStatusNullableJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::G.ImportStatus?>
{
/// <inheritdoc />
public override global::G.ImportStatus? Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case global::System.Text.Json.JsonTokenType.String:
{
var stringValue = reader.GetString();
if (stringValue != null)
{
return global::G.ImportStatusExtensions.ToEnum(stringValue);
}

break;
}
case global::System.Text.Json.JsonTokenType.Number:
{
var numValue = reader.GetInt32();
return (global::G.ImportStatus)numValue;
}
default:
throw new global::System.ArgumentOutOfRangeException(nameof(reader));
}

return default;
}

/// <inheritdoc />
public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
global::G.ImportStatus? value,
global::System.Text.Json.JsonSerializerOptions options)
{
writer = writer ?? throw new global::System.ArgumentNullException(nameof(writer));

if (value == null)
{
writer.WriteNullValue();
}
else
{
writer.WriteStringValue(global::G.ImportStatusExtensions.ToValueString(value.Value));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//HintName: JsonConverters.MigrationsGetStatusForOrgExcludeNullable.g.cs
#nullable enable

namespace OpenApiGenerator.JsonConverters
{
/// <inheritdoc />
public sealed class MigrationsGetStatusForOrgExcludeNullableJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::G.MigrationsGetStatusForOrgExclude?>
{
/// <inheritdoc />
public override global::G.MigrationsGetStatusForOrgExclude? Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case global::System.Text.Json.JsonTokenType.String:
{
var stringValue = reader.GetString();
if (stringValue != null)
{
return global::G.MigrationsGetStatusForOrgExcludeExtensions.ToEnum(stringValue);
}

break;
}
case global::System.Text.Json.JsonTokenType.Number:
{
var numValue = reader.GetInt32();
return (global::G.MigrationsGetStatusForOrgExclude)numValue;
}
default:
throw new global::System.ArgumentOutOfRangeException(nameof(reader));
}

return default;
}

/// <inheritdoc />
public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
global::G.MigrationsGetStatusForOrgExclude? value,
global::System.Text.Json.JsonSerializerOptions options)
{
writer = writer ?? throw new global::System.ArgumentNullException(nameof(writer));

if (value == null)
{
writer.WriteNullValue();
}
else
{
writer.WriteStringValue(global::G.MigrationsGetStatusForOrgExcludeExtensions.ToValueString(value.Value));
}
}
}
}
Loading

0 comments on commit 3e3f1f5

Please sign in to comment.