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

Support replacing methods #4547

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jorgerangel-msft
Copy link
Contributor

@jorgerangel-msft jorgerangel-msft commented Sep 26, 2024

This PR adds support for replacing generated methods with custom code. It also fixes several issues with constructing nullable types from custom code.

fixes: #4472

@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Sep 26, 2024
@azure-sdk
Copy link
Collaborator

API change check

API changes are not detected in this pull request.

@jorgerangel-msft jorgerangel-msft force-pushed the replace-method-member branch 2 times, most recently from b4b7d7c to c207660 Compare September 27, 2024 20:03
@jorgerangel-msft jorgerangel-msft changed the title Support replacing method members Support replacing methods Sep 27, 2024
@@ -4,7 +4,7 @@

namespace Sample.Models
{
public partial class MockInputModel
public partial class Model
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was running into build errors on my local due to this file path being too long. There's probably another overall fix we can do for that, but for now I'm shortening the filename.

{
public static partial class SampleNamespaceModelFactory
{
public static MockInputModel MockInputModel(string prop1 = default, bool? optionalBool = default)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add a test case where we have a nullable struct, where the struct is generated by the library?

}

string[] typeArguments = [.. namedTypeSymbol.TypeArguments.Select(GetFullyQualifiedName)];
return $"{nullableTypeName}`{namedTypeSymbol.TypeArguments.Length}[{string.Join(", ", typeArguments)}]";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the CSharpType we create from a symbol match the behavior for PropertyProvider.Type? I thought when creating CSharpType from the output types for a nullable, the CSharpType underlying Type would not be Nullable.

@@ -286,14 +285,11 @@ private CSharpType GetCSharpType(ITypeSymbol typeSymbol)
Type? type = System.Type.GetType(fullyQualifiedName);
if (type is null)
{
if (typeSymbol.TypeKind == TypeKind.Error)
throw new InvalidOperationException($"Unable to convert ITypeSymbol: {fullyQualifiedName} to a CSharpType in {Name}");

return ConstructCSharpTypeFromSymbol(typeSymbol, fullyQualifiedName, namedTypeSymbol);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we now will fall back to constructing from the symbol, can we remove the if block that checks if the name starts with the RootNamespace?

Copy link
Contributor

@JoshLove-msft JoshLove-msft Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, I don't think we want to do this as it will then attempt to look up generated types using GetType which would actually succeed if a custom partial class is added. But this would give us the wrong info for various properties of CSharpType.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support replacing a member in custom code (Methods)
3 participants