-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
ViewModel uses "+" splicing, and the swagger request body parameter is missing #2703
Comments
builder.Services.AddSwaggerGen(c =>
{
c.CustomSchemaIds(type => type.FullName.Replace("+", "."));
}); Got it |
I wish it's that simple. It brings another issue whenever a type contains generic parameter. Generic parameter has no |
@fakhrulhilal |
The solution works for this scenario [JsonDerivedType(typeof(Success))]
[JsonDerivedType(typeof(Partial))]
[JsonDerivedType(typeof(Failed))]
[JsonDerivedType(typeof(Success.WithValue<>))]
[JsonDerivedType(typeof(Partial.WithValue<>))]
[JsonDerivedType(typeof(Failed.WithValue<>))]
public abstract record UploadResult
{
private UploadResult(bool succeeded) {
Succeeded = succeeded;
}
public bool Succeeded { get; }
public abstract record FailedBase(string Reason) : UploadResult(false);
public sealed record Failed(string Reason) : FailedBase(Reason)
{
public sealed record WithNotUploaded(string Reason, IEnumerable<NonUploadedDoc> NotUploaded)
: FailedBase(Reason);
}
public abstract record SuccessBase(IEnumerable<UploadedDoc> Uploaded) : UploadResult(true);
public sealed record Success(IEnumerable<UploadedDoc> Uploaded) : SuccessBase(Uploaded)
{
public sealed record WithValue<T>(T Value, IEnumerable<UploadedDoc> Uploaded) : SuccessBase(Uploaded)
where T : struct;
}
public abstract record PartialBase(IEnumerable<UploadedDoc> Uploaded,
IEnumerable<NonUploadedDoc> NotUploaded) : SuccessBase(Uploaded);
public sealed record Partial(IEnumerable<UploadedDoc> Uploaded,
IEnumerable<NonUploadedDoc> NotUploaded) : PartialBase(Uploaded, NotUploaded)
{
public sealed record WithValue<T>(
T Value,
IEnumerable<UploadedDoc> Uploaded,
IEnumerable<NonUploadedDoc> NotUploaded)
: PartialBase(Uploaded, NotUploaded)
where T : struct;
}
}
[JsonDerivedType(typeof(Failed))]
[JsonDerivedType(typeof(Partial))]
[JsonDerivedType(typeof(Success))]
public abstract record CopyResult
{
public bool Succeeded { get; }
private CopyResult(bool succeeded) {
Succeeded = succeeded;
}
public sealed record Success(IEnumerable<AttachedDoc> Attached) : CopyResult(true);
public sealed record Partial
(IEnumerable<AttachedDoc> Attached, IEnumerable<NonAttachedDoc> NotAttached) : CopyResult(true);
public sealed record Failed
(string Reason, IEnumerable<NonAttachedDoc> NotAttached) : CopyResult(false);
}
public abstract record AttachedDocBase(int SourceDocumentId);
public sealed record NonAttachedDoc
(int SourceDocumentId, string Reason, string? Name = null) : AttachedDocBase(SourceDocumentId);
public sealed record AttachedDoc(int SourceDocumentId, string Name, int FileId) :
AttachedDocBase(SourceDocumentId); Notice that both I have solved this using combined solution from others ( private static string GenerateSchemaId(Type type) => type switch {
{ IsNested: true } when !string.IsNullOrEmpty(type.FullName) => type.FullName.Split('.')[^1].Remove("+", string.Empty),
{ IsGenericParameter: true, DeclaringType: not null } => $"{GenerateSchemaId(type.DeclaringType)}_{type.Name}",
_ => DefaultSchemaIdSelector(type)
};
private static string DefaultSchemaIdSelector(Type type) {
if (!type.IsConstructedGenericType) {
return type.Name.Replace("[]", "Array");
}
string prefix = type.GetGenericArguments()
.Select(DefaultSchemaIdSelector)
.Aggregate((previous, current) => previous + current);
return prefix + type.Name.Split('`').First();
} Then use it like usual I got the idea from this. |
If I change to "." splicing, the request body parameters are displayed normally
The text was updated successfully, but these errors were encountered: