-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added Unix timestamp detection.
- Loading branch information
Showing
100 changed files
with
1,902 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/libs/OpenApiGenerator.Core/Generation/Sources.JsonConverters.UnixTimestamp.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using OpenApiGenerator.Core.Json; | ||
using OpenApiGenerator.Core.Models; | ||
|
||
namespace OpenApiGenerator.Core.Generation; | ||
|
||
public static partial class Sources | ||
{ | ||
public static string GenerateUnixTimestampJsonConverter( | ||
Settings settings, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
if (settings.JsonSerializerType == JsonSerializerType.NewtonsoftJson) | ||
{ | ||
return $@"#nullable enable | ||
namespace OpenApiGenerator.JsonConverters | ||
{{ | ||
/// <inheritdoc /> | ||
public class UnixTimestampJsonConverter : global::Newtonsoft.Json.JsonConverter<global::System.DateTimeOffset> | ||
{{ | ||
/// <inheritdoc /> | ||
public override global::System.DateTimeOffset ReadJson( | ||
global::Newtonsoft.Json.JsonReader reader, | ||
global::System.Type objectType, | ||
global::System.DateTimeOffset existingValue, | ||
bool hasExistingValue, | ||
global::Newtonsoft.Json.JsonSerializer serializer) | ||
{{ | ||
if (reader.TokenType == global::Newtonsoft.Json.JsonToken.Integer) | ||
{{ | ||
switch (reader.Value) | ||
{{ | ||
case long unixTimestamp: | ||
return global::System.DateTimeOffset.FromUnixTimeSeconds(unixTimestamp); | ||
case int unixTimestampInt: | ||
return global::System.DateTimeOffset.FromUnixTimeSeconds(unixTimestampInt); | ||
}} | ||
}} | ||
return default; | ||
}} | ||
/// <inheritdoc /> | ||
public override void WriteJson( | ||
global::Newtonsoft.Json.JsonWriter writer, | ||
global::System.DateTimeOffset value, | ||
global::Newtonsoft.Json.JsonSerializer serializer) | ||
{{ | ||
writer.WriteValue(value.ToUnixTimeSeconds()); | ||
}} | ||
}} | ||
}} | ||
"; | ||
} | ||
|
||
return $@"#nullable enable | ||
namespace OpenApiGenerator.JsonConverters | ||
{{ | ||
/// <inheritdoc /> | ||
public class UnixTimestampJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::System.DateTimeOffset> | ||
{{ | ||
/// <inheritdoc /> | ||
public override global::System.DateTimeOffset Read( | ||
ref global::System.Text.Json.Utf8JsonReader reader, | ||
global::System.Type typeToConvert, | ||
global::System.Text.Json.JsonSerializerOptions options) | ||
{{ | ||
if (reader.TokenType == global::System.Text.Json.JsonTokenType.Number) | ||
{{ | ||
if (reader.TryGetInt64(out long unixTimestamp)) | ||
{{ | ||
return global::System.DateTimeOffset.FromUnixTimeSeconds(unixTimestamp); | ||
}} | ||
if (reader.TryGetInt32(out int unixTimestampInt)) | ||
{{ | ||
return global::System.DateTimeOffset.FromUnixTimeSeconds(unixTimestampInt); | ||
}} | ||
}} | ||
return default; | ||
}} | ||
/// <inheritdoc /> | ||
public override void Write( | ||
global::System.Text.Json.Utf8JsonWriter writer, | ||
global::System.DateTimeOffset value, | ||
global::System.Text.Json.JsonSerializerOptions options) | ||
{{ | ||
long unixTimestamp = value.ToUnixTimeSeconds(); | ||
writer.WriteNumberValue(unixTimestamp); | ||
}} | ||
}} | ||
}} | ||
"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
....SnapshotTests/Snapshots/Ai21/NewtonsoftJson/_#JsonConverters.UnixTimestamp.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//HintName: JsonConverters.UnixTimestamp.g.cs | ||
#nullable enable | ||
|
||
namespace OpenApiGenerator.JsonConverters | ||
{ | ||
/// <inheritdoc /> | ||
public class UnixTimestampJsonConverter : global::Newtonsoft.Json.JsonConverter<global::System.DateTimeOffset> | ||
{ | ||
/// <inheritdoc /> | ||
public override global::System.DateTimeOffset ReadJson( | ||
global::Newtonsoft.Json.JsonReader reader, | ||
global::System.Type objectType, | ||
global::System.DateTimeOffset existingValue, | ||
bool hasExistingValue, | ||
global::Newtonsoft.Json.JsonSerializer serializer) | ||
{ | ||
if (reader.TokenType == global::Newtonsoft.Json.JsonToken.Integer) | ||
{ | ||
switch (reader.Value) | ||
{ | ||
case long unixTimestamp: | ||
return global::System.DateTimeOffset.FromUnixTimeSeconds(unixTimestamp); | ||
case int unixTimestampInt: | ||
return global::System.DateTimeOffset.FromUnixTimeSeconds(unixTimestampInt); | ||
} | ||
} | ||
|
||
return default; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void WriteJson( | ||
global::Newtonsoft.Json.JsonWriter writer, | ||
global::System.DateTimeOffset value, | ||
global::Newtonsoft.Json.JsonSerializer serializer) | ||
{ | ||
writer.WriteValue(value.ToUnixTimeSeconds()); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
....SnapshotTests/Snapshots/Ai21/SystemTextJson/_#JsonConverters.UnixTimestamp.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//HintName: JsonConverters.UnixTimestamp.g.cs | ||
#nullable enable | ||
|
||
namespace OpenApiGenerator.JsonConverters | ||
{ | ||
/// <inheritdoc /> | ||
public class UnixTimestampJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::System.DateTimeOffset> | ||
{ | ||
/// <inheritdoc /> | ||
public override global::System.DateTimeOffset Read( | ||
ref global::System.Text.Json.Utf8JsonReader reader, | ||
global::System.Type typeToConvert, | ||
global::System.Text.Json.JsonSerializerOptions options) | ||
{ | ||
if (reader.TokenType == global::System.Text.Json.JsonTokenType.Number) | ||
{ | ||
if (reader.TryGetInt64(out long unixTimestamp)) | ||
{ | ||
return global::System.DateTimeOffset.FromUnixTimeSeconds(unixTimestamp); | ||
} | ||
if (reader.TryGetInt32(out int unixTimestampInt)) | ||
{ | ||
return global::System.DateTimeOffset.FromUnixTimeSeconds(unixTimestampInt); | ||
} | ||
} | ||
|
||
return default; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Write( | ||
global::System.Text.Json.Utf8JsonWriter writer, | ||
global::System.DateTimeOffset value, | ||
global::System.Text.Json.JsonSerializerOptions options) | ||
{ | ||
long unixTimestamp = value.ToUnixTimeSeconds(); | ||
writer.WriteNumberValue(unixTimestamp); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...shotTests/Snapshots/Anthropic/NewtonsoftJson/_#JsonConverters.UnixTimestamp.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//HintName: JsonConverters.UnixTimestamp.g.cs | ||
#nullable enable | ||
|
||
namespace OpenApiGenerator.JsonConverters | ||
{ | ||
/// <inheritdoc /> | ||
public class UnixTimestampJsonConverter : global::Newtonsoft.Json.JsonConverter<global::System.DateTimeOffset> | ||
{ | ||
/// <inheritdoc /> | ||
public override global::System.DateTimeOffset ReadJson( | ||
global::Newtonsoft.Json.JsonReader reader, | ||
global::System.Type objectType, | ||
global::System.DateTimeOffset existingValue, | ||
bool hasExistingValue, | ||
global::Newtonsoft.Json.JsonSerializer serializer) | ||
{ | ||
if (reader.TokenType == global::Newtonsoft.Json.JsonToken.Integer) | ||
{ | ||
switch (reader.Value) | ||
{ | ||
case long unixTimestamp: | ||
return global::System.DateTimeOffset.FromUnixTimeSeconds(unixTimestamp); | ||
case int unixTimestampInt: | ||
return global::System.DateTimeOffset.FromUnixTimeSeconds(unixTimestampInt); | ||
} | ||
} | ||
|
||
return default; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void WriteJson( | ||
global::Newtonsoft.Json.JsonWriter writer, | ||
global::System.DateTimeOffset value, | ||
global::Newtonsoft.Json.JsonSerializer serializer) | ||
{ | ||
writer.WriteValue(value.ToUnixTimeSeconds()); | ||
} | ||
} | ||
} |
Oops, something went wrong.