Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,18 @@ private MethodProvider BuildWriteObjectValueMethodGeneric()
// int case
BuildWriteObjectValueSwitchCase(typeof(int), "i",
i => new MethodBodyStatement[] { writer.WriteNumberValue(i), Break }),
// short case
BuildWriteObjectValueSwitchCase(typeof(short), "shortValue",
shortValue => new MethodBodyStatement[] { writer.WriteNumberValue(shortValue), Break }),
// ushort case
BuildWriteObjectValueSwitchCase(typeof(ushort), "ushortValue",
ushortValue => new MethodBodyStatement[] { writer.WriteNumberValue(ushortValue), Break }),
// uint case
BuildWriteObjectValueSwitchCase(typeof(uint), "uintValue",
uintValue => new MethodBodyStatement[] { writer.WriteNumberValue(uintValue), Break }),
// ulong case
BuildWriteObjectValueSwitchCase(typeof(ulong), "ulongValue",
ulongValue => new MethodBodyStatement[] { writer.WriteNumberValue(ulongValue), Break }),
Comment thread
jorgerangel-msft marked this conversation as resolved.
// decimal case
BuildWriteObjectValueSwitchCase(typeof(decimal), "d",
dec => new MethodBodyStatement[] { writer.WriteNumberValue(dec), Break }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ private static ValueExpression CreateXmlSerializePrimitiveExpression(ValueExpres
{
return valueType.FrameworkType switch
{
Type t when t == typeof(ulong) => value.CastTo(typeof(decimal)),
Comment thread
jorgerangel-msft marked this conversation as resolved.
Type t when t == typeof(DateTimeOffset) => serializationFormat switch
{
_ => value.Invoke(nameof(ToString), Literal(serializationFormat.ToFormatSpecifier()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2753,8 +2753,11 @@ Type t when ValueTypeIsInt(t) =>

private static bool ValueTypeIsInt(Type valueType) =>
valueType == typeof(long) ||
valueType == typeof(ulong) ||
valueType == typeof(int) ||
valueType == typeof(uint) ||
valueType == typeof(short) ||
valueType == typeof(ushort) ||
valueType == typeof(sbyte) ||
valueType == typeof(byte);

Expand All @@ -2772,8 +2775,11 @@ private static bool ValueTypeIsNumber(Type valueType) =>
_ => type switch
{
Type t when t == typeof(long) => element.GetInt64(),
Type t when t == typeof(ulong) => element.GetUInt64(),
Type t when t == typeof(int) => element.GetInt32(),
Type t when t == typeof(uint) => element.GetUInt32(),
Type t when t == typeof(short) => element.GetInt16(),
Type t when t == typeof(ushort) => element.GetUInt16(),
Type t when t == typeof(sbyte) => element.GetSByte(),
Type t when t == typeof(byte) => element.GetByte(),
_ => throw new NotSupportedException($"Framework type {type} is not int.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ public static class JsonElementSnippets
public static ValueExpression GetSByte(this ScopedApi<JsonElement> jsonElement) => jsonElement.Invoke(nameof(JsonElement.GetSByte));
public static ValueExpression GetByte(this ScopedApi<JsonElement> jsonElement) => jsonElement.Invoke(nameof(JsonElement.GetByte));
public static ValueExpression GetInt16(this ScopedApi<JsonElement> jsonElement) => jsonElement.Invoke(nameof(JsonElement.GetInt16));
public static ValueExpression GetUInt16(this ScopedApi<JsonElement> jsonElement) => jsonElement.Invoke(nameof(JsonElement.GetUInt16));
public static ValueExpression GetInt32(this ScopedApi<JsonElement> jsonElement) => jsonElement.Invoke(nameof(JsonElement.GetInt32));
public static ValueExpression GetUInt32(this ScopedApi<JsonElement> jsonElement) => jsonElement.Invoke(nameof(JsonElement.GetUInt32));
public static ValueExpression GetInt64(this ScopedApi<JsonElement> jsonElement) => jsonElement.Invoke(nameof(JsonElement.GetInt64));
public static ValueExpression GetUInt64(this ScopedApi<JsonElement> jsonElement) => jsonElement.Invoke(nameof(JsonElement.GetUInt64));
public static ScopedApi<string> GetRawText(this ScopedApi<JsonElement> jsonElement) => jsonElement.Invoke(nameof(JsonElement.GetRawText)).As<string>();
public static ValueExpression GetSingle(this ScopedApi<JsonElement> jsonElement) => jsonElement.Invoke(nameof(JsonElement.GetSingle));
public static ScopedApi<string> GetString(this ScopedApi<JsonElement> jsonElement) => jsonElement.Invoke(nameof(JsonElement.GetString)).As<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ public static void WriteObjectValue<T>(this global::System.Text.Json.Utf8JsonWri
case int i:
writer.WriteNumberValue(i);
break;
case short shortValue:
writer.WriteNumberValue(shortValue);
break;
case ushort ushortValue:
writer.WriteNumberValue(ushortValue);
break;
case uint uintValue:
writer.WriteNumberValue(uintValue);
break;
case ulong ulongValue:
writer.WriteNumberValue(ulongValue);
break;
case decimal d:
writer.WriteNumberValue(d);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ public static void WriteObjectValue<T>(this global::System.Text.Json.Utf8JsonWri
case int i:
writer.WriteNumberValue(i);
break;
case short shortValue:
writer.WriteNumberValue(shortValue);
break;
case ushort ushortValue:
writer.WriteNumberValue(ushortValue);
break;
case uint uintValue:
writer.WriteNumberValue(uintValue);
break;
case ulong ulongValue:
writer.WriteNumberValue(ulongValue);
break;
case decimal d:
writer.WriteNumberValue(d);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -950,13 +950,13 @@ public void TestIntSerializationStatement(
InputPrimitiveTypeKind.Integer,
InputPrimitiveTypeKind.SafeInt,
InputPrimitiveTypeKind.Int8,
// InputPrimitiveTypeKind.Int16, TODO: add them back when we decide the exact corresponding type
InputPrimitiveTypeKind.Int16,
InputPrimitiveTypeKind.Int32,
InputPrimitiveTypeKind.Int64,
InputPrimitiveTypeKind.UInt8
// InputPrimitiveTypeKind.UInt16,
// InputPrimitiveTypeKind.UInt32,
// InputPrimitiveTypeKind.UInt64
InputPrimitiveTypeKind.UInt8,
InputPrimitiveTypeKind.UInt16,
InputPrimitiveTypeKind.UInt32,
InputPrimitiveTypeKind.UInt64
)] InputPrimitiveTypeKind kind,
[Values("string", null)] string encode)
{
Expand Down Expand Up @@ -1010,11 +1010,17 @@ public void TestBooleanDeserialization(string? encode)
[TestCase(typeof(long), SerializationFormat.Int_String, ExpectedResult = "long.Parse(foo.GetString())")]
[TestCase(typeof(int), SerializationFormat.Int_String, ExpectedResult = "int.Parse(foo.GetString())")]
[TestCase(typeof(short), SerializationFormat.Int_String, ExpectedResult = "short.Parse(foo.GetString())")]
[TestCase(typeof(ushort), SerializationFormat.Int_String, ExpectedResult = "ushort.Parse(foo.GetString())")]
[TestCase(typeof(uint), SerializationFormat.Int_String, ExpectedResult = "uint.Parse(foo.GetString())")]
[TestCase(typeof(ulong), SerializationFormat.Int_String, ExpectedResult = "ulong.Parse(foo.GetString())")]
[TestCase(typeof(byte), SerializationFormat.Int_String, ExpectedResult = "byte.Parse(foo.GetString())")]
[TestCase(typeof(sbyte), SerializationFormat.Int_String, ExpectedResult = "sbyte.Parse(foo.GetString())")]
[TestCase(typeof(long), SerializationFormat.Default, ExpectedResult = "foo.GetInt64()")]
[TestCase(typeof(int), SerializationFormat.Default, ExpectedResult = "foo.GetInt32()")]
[TestCase(typeof(short), SerializationFormat.Default, ExpectedResult = "foo.GetInt16()")]
[TestCase(typeof(ushort), SerializationFormat.Default, ExpectedResult = "foo.GetUInt16()")]
[TestCase(typeof(uint), SerializationFormat.Default, ExpectedResult = "foo.GetUInt32()")]
[TestCase(typeof(ulong), SerializationFormat.Default, ExpectedResult = "foo.GetUInt64()")]
[TestCase(typeof(byte), SerializationFormat.Default, ExpectedResult = "foo.GetByte()")]
[TestCase(typeof(sbyte), SerializationFormat.Default, ExpectedResult = "foo.GetSByte()")]
public string TestIntDeserializeExpression(Type type, SerializationFormat format)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,10 @@ public async Task PersistableModelWriteCoreHandlesJsonAndXmlFormats()
}

[TestCase(typeof(int), SerializationFormat.Default, ExpectedResult = "writer.WriteValue(value);\n")]
[TestCase(typeof(short), SerializationFormat.Default, ExpectedResult = "writer.WriteValue(value);\n")]
[TestCase(typeof(ushort), SerializationFormat.Default, ExpectedResult = "writer.WriteValue(value);\n")]
[TestCase(typeof(uint), SerializationFormat.Default, ExpectedResult = "writer.WriteValue(value);\n")]
[TestCase(typeof(ulong), SerializationFormat.Default, ExpectedResult = "writer.WriteValue(((decimal)value));\n")]
[TestCase(typeof(string), SerializationFormat.Default, ExpectedResult = "writer.WriteValue(value);\n")]
[TestCase(typeof(bool), SerializationFormat.Default, ExpectedResult = "writer.WriteValue(value);\n")]
[TestCase(typeof(long), SerializationFormat.Default, ExpectedResult = "writer.WriteValue(value);\n")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,12 @@ protected virtual bool IsLastContractModelBasePropertyCompatibleCore(
InputPrimitiveTypeKind.Float64 => typeof(double),
InputPrimitiveTypeKind.Int8 => typeof(sbyte),
InputPrimitiveTypeKind.UInt8 => typeof(byte),
InputPrimitiveTypeKind.Int16 => typeof(short),
InputPrimitiveTypeKind.UInt16 => typeof(ushort),
InputPrimitiveTypeKind.Int32 => typeof(int),
InputPrimitiveTypeKind.UInt32 => typeof(uint),
InputPrimitiveTypeKind.Int64 => typeof(long),
InputPrimitiveTypeKind.UInt64 => typeof(ulong),
Comment thread
jorgerangel-msft marked this conversation as resolved.
InputPrimitiveTypeKind.SafeInt => typeof(long),
InputPrimitiveTypeKind.Integer => typeof(long), // in typespec, integer is the base type of int related types, see type relation: https://typespec.io/docs/language-basics/type-relations
InputPrimitiveTypeKind.Float => typeof(double), // in typespec, float is the base type of float32 and float64, see type relation: https://typespec.io/docs/language-basics/type-relations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Net;
using System.Text.Json;
using System.Threading.Tasks;
Expand Down Expand Up @@ -131,6 +132,35 @@ public void CreateEnum_WithDeclaringType()
Assert.IsFalse(ReferenceEquals(actual3, actual2));
}

[TestCase(InputPrimitiveTypeKind.Int8, typeof(sbyte))]
[TestCase(InputPrimitiveTypeKind.Int16, typeof(short))]
[TestCase(InputPrimitiveTypeKind.Int32, typeof(int))]
[TestCase(InputPrimitiveTypeKind.Int64, typeof(long))]
[TestCase(InputPrimitiveTypeKind.UInt8, typeof(byte))]
[TestCase(InputPrimitiveTypeKind.UInt16, typeof(ushort))]
[TestCase(InputPrimitiveTypeKind.UInt32, typeof(uint))]
[TestCase(InputPrimitiveTypeKind.UInt64, typeof(ulong))]
[TestCase(InputPrimitiveTypeKind.String, typeof(string))]
[TestCase(InputPrimitiveTypeKind.Boolean, typeof(bool))]
[TestCase(InputPrimitiveTypeKind.Unknown, typeof(BinaryData))]
public void CreateCSharpType_PrimitiveAndCollections(InputPrimitiveTypeKind kind, Type expectedType)
{
var name = kind.ToString().ToLowerInvariant();
var input = new InputPrimitiveType(kind, name, $"TypeSpec.{name}");
var factory = CodeModelGenerator.Instance.TypeFactory;

Assert.Multiple(() =>
{
Assert.AreEqual(new CSharpType(expectedType), factory.CreateCSharpType(input));
Assert.AreEqual(new CSharpType(expectedType, isNullable: true),
factory.CreateCSharpType(new InputNullableType(input)));
Assert.AreEqual(new CSharpType(typeof(IList<>), expectedType),
factory.CreateCSharpType(new InputArrayType("Array", "TypeSpec.Array", input)));
Assert.AreEqual(new CSharpType(typeof(IDictionary<,>), typeof(string), expectedType),
factory.CreateCSharpType(new InputDictionaryType("Record", InputPrimitiveType.String, input)));
});
}

[Test]
public void IntSerializationFormat([Values(
InputPrimitiveTypeKind.Integer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ namespace TestProjects.Local.Tests
{
public class ModelSerializationExtensionsTests
{
[TestCase(short.MinValue, "-32768")]
[TestCase(short.MaxValue, "32767")]
[TestCase(ushort.MinValue, "0")]
[TestCase(ushort.MaxValue, "65535")]
[TestCase(uint.MinValue, "0")]
[TestCase(uint.MaxValue, "4294967295")]
[TestCase(ulong.MinValue, "0")]
[TestCase(ulong.MaxValue, "18446744073709551615")]
public void WriteObjectValuePreservesIntegerRange(object value, string expected)
{
var buffer = new ArrayBufferWriter<byte>();
using var writer = new Utf8JsonWriter(buffer);

writer.WriteObjectValue(value);
writer.Flush();

Assert.AreEqual(expected, Encoding.UTF8.GetString(buffer.WrittenSpan));
}

[TestCase("{ \"text\": \"こんにちは 🌍\", \"escaped\": \"\\u0061\" }")]
[TestCase("[1, true, null, {\"nested\": []}]")]
[TestCase("\"hello\"")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,18 @@ public static void WriteObjectValue<T>(this Utf8JsonWriter writer, T value, Mode
case int i:
writer.WriteNumberValue(i);
break;
case short shortValue:
writer.WriteNumberValue(shortValue);
break;
case ushort ushortValue:
writer.WriteNumberValue(ushortValue);
break;
case uint uintValue:
writer.WriteNumberValue(uintValue);
break;
case ulong ulongValue:
writer.WriteNumberValue(ulongValue);
break;
case decimal d:
writer.WriteNumberValue(d);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public Task Encode_Numeric_Property_uint32AsStringOptional() => Test(async (host
{
var response = await new NumericClient(host, null).GetPropertyClient().Uint32AsStringOptionalAsync(new Uint32AsStringProperty()
{
Value = "1"
Value = 1
});
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual("1", response.Value.Value);
Assert.AreEqual(1u, response.Value.Value);
});

[SpectorTest]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static partial class EncodeNumericModelFactory
{
public static SafeintAsStringProperty SafeintAsStringProperty(long value = default) => throw null;

public static Uint32AsStringProperty Uint32AsStringProperty(object value = default) => throw null;
public static Uint32AsStringProperty Uint32AsStringProperty(uint? value = default) => throw null;

public static Uint8AsStringProperty Uint8AsStringProperty(byte value = default) => throw null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public partial class Uint32AsStringProperty
{
public Uint32AsStringProperty() => throw null;

public object Value
public uint? Value
{
get => throw null;
set => throw null;
Expand Down
Loading