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

Is it possible to support Optional<string> #133

Open
CRC32EX opened this issue Nov 11, 2023 · 4 comments
Open

Is it possible to support Optional<string> #133

CRC32EX opened this issue Nov 11, 2023 · 4 comments

Comments

@CRC32EX
Copy link

CRC32EX commented Nov 11, 2023

Server code

// DB Model
class User
{
  public uint Id { get; set; }
  public string? StatusMessage { get; set; } // nullable
}

// HTTP Model
[TranspilationSource]
class UpdateUserRequest 
{
  public int UserId { get; set; }
  [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
  public Optional<string?> StatusMessage { get; set; }
}

// Controller
[HttpPatch]
public IActionResult UpdateUser(UpdateUserRequest request)
{
  var user = DB.FindUserById(request.UserId);

  // if status_message is provided, update it
  if (request.StatusMessage.HasValue) {
    user.StatusMessage = request.StatusMessage;
    DB.Update(user);
  }

  // if (request.Prefecture.HasValue) { /* ... */ }
  // if (request.FirstName.HasValue) { /* ... */ }
  // if (request.LastName.HasValue) { /* ... */ }
  // if (request.BirthDay.HasValue) { /* ... */ }
  // ...
}

HTTP Request example

PATCH /api/user HTTP/1.1
Content-Type: application/json

{ "user_id": 1, "status_message": "I'm fine" }

I want to know JSON string parameter is null or undefined.
For example,

Request Update DB?
{ "user_id": 1, "status_message": "I'm fine" } Yes. Value is I'm fine
{ "user_id": 1, "status_message": "" } Yes. Value is ``
{ "user_id": 1, "status_message": null } Yes. Value is null
{ "user_id": 1 } No.

But, Tapper is not supported it.
Could you support it?
Or, do you have some other good ideas?

Ref:

@CRC32EX CRC32EX changed the title Is it possible to support Optional<string> ? Is it possible to support Optional<string> Nov 11, 2023
@nenoNaninu
Copy link
Owner

nenoNaninu commented Nov 12, 2023

The class Optional<T> does not exist in the standard library. It would help if you showed an implementation of Optional<T>.
I assume that Optional<T> is a type like System.Nullable<T>, but there are two challenges.

First, Tapper needs to support user-defined generics types. This feature could be done with time.

Second, you must implement a custom JsonConverter to get the desired JSON string. And since custom JsonConverter is highly flexible, it is difficult for Tapper to support it.

@CRC32EX
Copy link
Author

CRC32EX commented Nov 12, 2023

Optional<T> implementation is here.

How about implement attribute, that force emit TypeScript type.
Like TranspilationSourceForceType.

Input

// UpdateUserRequest.cs
[TranspilationSource]
class UpdateUserRequest 
{
  public int UserId { get; set; }

  // [TranspilationSourceForceType(Type = TypeScriptType.String | TypeScriptType.Null | TypeScriptType.Undefined)]
  [TranspilationSourceForceType(TypeString = "(string | undefined)", Nullable = true)]
  [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
  public Optional<string?> StatusMessage { get; set; }
}

Output

// UpdateUserRequest.ts
/** Transpiled from UpdateUserRequest */
export type ConfigInfo = {
  /** Transpiled from uint */
  userId: number;
  /** Transpiled from unknown(Defined by user) */
  statusMessage?: (string | undefined);
}

@nenoNaninu
Copy link
Owner

It seems like a good idea.

@hahn-kev
Copy link

I would also like to be able to specify the explicit output type. I've got a struct that wraps a string, when serialized it's just turned into a string so I'd like to represent that in TS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants