Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate AdditionalProperties with raw data field (Phase 1) #4510

Merged
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
14f92fc
initial commit to change the unverifiable types to binarydata
ArcturusZhang Apr 1, 2024
69f05f4
update the structure to split additional properties and raw data field
ArcturusZhang Apr 1, 2024
03dc014
additional properties and raw data field could coexist now, but still…
ArcturusZhang Apr 1, 2024
4d673cb
implement the filter for additional properties property
ArcturusZhang Apr 2, 2024
0788602
regenerate
ArcturusZhang Apr 2, 2024
d59edf3
Merge remote-tracking branch 'origin/feature/v3' into phase1-of-addit…
ArcturusZhang Apr 2, 2024
78b7021
update for array and dictionary
ArcturusZhang Apr 2, 2024
eac9ae1
fix issue in some hlc package
ArcturusZhang Apr 2, 2024
0160fa7
fix in HLC when unknown is object there
ArcturusZhang Apr 2, 2024
8f0ce57
update schema object type accordingly
ArcturusZhang Apr 3, 2024
ba7a8d2
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang Apr 3, 2024
3ebade9
regen
ArcturusZhang Apr 3, 2024
03fc0e3
Merge remote-tracking branch 'origin/feature/v3' into phase1-of-addit…
ArcturusZhang Apr 15, 2024
0c5b0df
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang Apr 15, 2024
067b458
reenable some test cases, and fix the emitter issue to support ...Record
ArcturusZhang Apr 15, 2024
2d1d1f3
Merge remote-tracking branch 'origin/feature/v3' into phase1-of-addit…
ArcturusZhang Apr 16, 2024
67c2948
Merge remote-tracking branch 'origin/feature/v3' into phase1-of-addit…
ArcturusZhang Apr 16, 2024
dda7386
regen after merge
ArcturusZhang Apr 16, 2024
e09d2c1
support generate additional properties from record spread and add tes…
ArcturusZhang Apr 16, 2024
3225943
added an array case
ArcturusZhang Apr 16, 2024
639f331
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang Apr 17, 2024
7dcb00c
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang Apr 19, 2024
b8217fe
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang Apr 19, 2024
2477c6c
regen
ArcturusZhang Apr 19, 2024
9fe5d71
fix the union type issue
ArcturusZhang Apr 19, 2024
fac9c3d
fix the replace BinaryData with union issue
ArcturusZhang Apr 19, 2024
b79d0c9
fix
ArcturusZhang Apr 19, 2024
0ace40a
update cadl-ranch version and implement half of the new test cases
ArcturusZhang Apr 19, 2024
227ad89
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang Apr 22, 2024
a304a2e
add other cases
ArcturusZhang Apr 22, 2024
73c1fcf
fix failed case
ArcturusZhang Apr 22, 2024
e1c8005
regen
ArcturusZhang Apr 22, 2024
b4bc44d
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang Apr 22, 2024
ade8950
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang Apr 23, 2024
13aaeb8
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang Apr 23, 2024
2f76d6f
Merge remote-tracking branch 'origin/feature/v3' into phase1-of-addit…
ArcturusZhang Apr 28, 2024
c7b58db
remove workaround
ArcturusZhang Apr 28, 2024
b6c2f60
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang Apr 28, 2024
f200044
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang Apr 28, 2024
b2a089b
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang Apr 29, 2024
264e081
regen after merge
ArcturusZhang Apr 29, 2024
2fc5219
adding similar things to schemaobjecttype
ArcturusZhang Apr 29, 2024
c4333fd
fix test cases
ArcturusZhang Apr 29, 2024
2324e7f
fix a disalignment
ArcturusZhang Apr 29, 2024
b46505d
Merge remote-tracking branch 'origin/feature/v3' into phase1-of-addit…
ArcturusZhang Apr 29, 2024
891187a
update a mgmt test to ensure this is working properly
ArcturusZhang Apr 29, 2024
99b7642
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang Apr 29, 2024
29431c2
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang Apr 30, 2024
166307d
Update src/AutoRest.CSharp/Common/Output/Builders/BuilderHelpers.cs
ArcturusZhang Apr 30, 2024
c46deac
Update src/AutoRest.CSharp/Common/Output/Builders/BuilderHelpers.cs
ArcturusZhang Apr 30, 2024
a050cfd
Merge remote-tracking branch 'origin/feature/v3' into phase1-of-addit…
ArcturusZhang May 7, 2024
8cdc52b
Merge remote-tracking branch 'origin/feature/v3' into phase1-of-addit…
ArcturusZhang May 9, 2024
34585c3
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang May 10, 2024
787ff34
Merge branch 'feature/v3' into phase1-of-additionalproperties
ArcturusZhang May 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/AutoRest.CSharp/Common/Generation/Types/TypeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using AutoRest.CSharp.Common.Input;
using AutoRest.CSharp.Common.Output.Models.Types;
using AutoRest.CSharp.Input;
using AutoRest.CSharp.Output.Models.Shared;
using AutoRest.CSharp.Output.Models.Types;
using Azure;
using Azure.Core;
Expand All @@ -25,12 +22,13 @@ namespace AutoRest.CSharp.Generation.Types
internal class TypeFactory
{
private readonly OutputLibrary _library;
private readonly Type _unknownType;

public Type UnknownType { get; }

public TypeFactory(OutputLibrary library, Type unknownType)
{
_library = library;
_unknownType = unknownType;
UnknownType = unknownType;
}

private Type AzureResponseErrorType => typeof(ResponseError);
Expand Down Expand Up @@ -93,7 +91,7 @@ public TypeFactory(OutputLibrary library, Type unknownType)
_ => new CSharpType(typeof(object), inputType.IsNullable),
},
InputGenericType genericType => new CSharpType(genericType.Type, CreateType(genericType.ArgumentType)).WithNullable(inputType.IsNullable),
InputIntrinsicType { Kind: InputIntrinsicTypeKind.Unknown } => _unknownType,
InputIntrinsicType { Kind: InputIntrinsicTypeKind.Unknown } => UnknownType,
CodeModelType cmt => CreateType(cmt.Schema, cmt.IsNullable),
_ => throw new Exception("Unknown type")
};
Expand Down Expand Up @@ -141,8 +139,8 @@ private Type GetListType(Schema schema)
AllSchemaTypes.Unixtime => typeof(DateTimeOffset),
AllSchemaTypes.Uri => typeof(Uri),
AllSchemaTypes.Uuid => typeof(Guid),
AllSchemaTypes.Any => _unknownType,
AllSchemaTypes.AnyObject => ToXMsFormatType(format) ?? _unknownType,
AllSchemaTypes.Any => UnknownType,
AllSchemaTypes.AnyObject => ToXMsFormatType(format) ?? UnknownType,
AllSchemaTypes.Binary => typeof(byte[]),
_ => null
};
Expand Down
62 changes: 61 additions & 1 deletion src/AutoRest.CSharp/Common/Output/Builders/BuilderHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security;
using System.Text;
using AutoRest.CSharp.Common.Input;
using AutoRest.CSharp.Generation.Types;
Expand Down Expand Up @@ -358,5 +357,66 @@ public static MethodSignatureModifiers MapModifiers(ISymbol symbol)
}
return modifiers;
}

public static CSharpType CreateAdditionalPropertiesPropertyType(CSharpType originalType, CSharpType unknownType)
{
// TODO -- we only construct additional properties when the type is verifiable, because we always need the property to fall into the bucket of serialized additional raw data field when it does not fit the additional properties.
var arguments = originalType.Arguments;
var keyType = arguments[0];
var valueType = arguments[1];

return originalType.MakeGenericType(new[] { ReplaceUnverifiableType(keyType, unknownType), ReplaceUnverifiableType(valueType, unknownType) });
}

private static CSharpType ReplaceUnverifiableType(CSharpType type, CSharpType unknownType)
{
// when the type is System.Object
ArcturusZhang marked this conversation as resolved.
Show resolved Hide resolved
if (type.EqualsIgnoreNullable(unknownType))
{
return type;
}

// when the type is a verifiable type
if (BuilderHelpers.IsVerifiableType(type))
ArcturusZhang marked this conversation as resolved.
Show resolved Hide resolved
{
return type;
}

// when the type is a union
if (type.IsUnion)
{
return type;
}

// otherwise the type is not a verifiable type
// replace for list
if (type.IsList)
{
return type.MakeGenericType(new[] { ReplaceUnverifiableType(type.Arguments[0], unknownType) });
}
// replace for dictionary
if (type.IsDictionary)
{
return type.MakeGenericType(new[] { ReplaceUnverifiableType(type.Arguments[0], unknownType), ReplaceUnverifiableType(type.Arguments[1], unknownType) });
}
// for the other cases, wrap them in a union
return CSharpType.FromUnion(new[] { type }, type.IsNullable);
}

private static readonly HashSet<Type> _verifiableTypes = new HashSet<Type>
{
// The following types are constructed by the `TryGetXXX` methods on `JsonElement`.
typeof(byte), typeof(byte[]), typeof(sbyte),
typeof(DateTime), typeof(DateTimeOffset),
typeof(decimal), typeof(double), typeof(short), typeof(int), typeof(long), typeof(float),
typeof(ushort), typeof(uint), typeof(ulong),
typeof(Guid),
// The following types have a firm JsonValueKind to verify
typeof(string), typeof(bool)
};

public static bool IsVerifiableType(Type type) => _verifiableTypes.Contains(type);

public static bool IsVerifiableType(CSharpType type) => type is { IsFrameworkType: true, FrameworkType: { } frameworkType } && IsVerifiableType(frameworkType);
}
}