diff --git a/backend/Sources/Controllers/PostsController.cs b/backend/Sources/Controllers/PostsController.cs index 7ea3543..dfa52c3 100644 --- a/backend/Sources/Controllers/PostsController.cs +++ b/backend/Sources/Controllers/PostsController.cs @@ -96,7 +96,7 @@ public async Task>> GetPostsByPar [ProducesResponseType(Status401Unauthorized)] [ProducesResponseType(Status500InternalServerError)] [Authorize] - public async Task AddPost(PostSubmissionDto post) + public async Task AddPost([FromForm] PostSubmissionDto post) { Claim? userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier); if (userIdClaim is null) @@ -109,8 +109,12 @@ public async Task AddPost(PostSubmissionDto post) { return Problem("The account of the currently logged in user has been deleted.", statusCode: Status401Unauthorized); } - await AddMissingTagsToDb(post.Tags); - ICollection tags = await context.Tags.Where(tag => post.Tags.Contains(tag.Name)).ToListAsync(); + // This is a compatibility fix for a bug in Swagger UI where if you use OpenAPI 3, arrays sent as form fields are sent as comma-separated lists instead of one element per field. + // https://github.com/swagger-api/swagger-ui/issues/10221 + IEnumerable tagsString = post.Tags.SelectMany(tag => tag.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)); + await AddMissingTagsToDb(tagsString); + // Since AddMissingTagsToDb never adds duplicate tags to the DB, this part will also not have duplicate tags. + ICollection tags = await context.Tags.Where(tag => tagsString.Contains(tag.Name)).ToListAsync(); Post newPost = new() { Author = author, @@ -125,9 +129,10 @@ public async Task AddPost(PostSubmissionDto post) return CreatedAtAction(nameof(GetPostById), new { id = newPost.Id }, null); } - private async Task AddMissingTagsToDb(ICollection tagsToAddToNewPost) + private async Task AddMissingTagsToDb(IEnumerable tagsToAddToNewPost) { IEnumerable existingTagNames = context.Tags.Where(tag => tagsToAddToNewPost.Contains(tag.Name)).Select(tag => tag.Name); + // Except only returns unique elements, so we will never add duplicate tags. IEnumerable missingTagNames = tagsToAddToNewPost.Except(existingTagNames); TagType topicTagType = new() { Id = TagTypeId.Topic, Name = "topic" }; context.TagTypes.Attach(topicTagType); diff --git a/backend/Sources/Program.cs b/backend/Sources/Program.cs index 1450c5a..0be4f59 100644 --- a/backend/Sources/Program.cs +++ b/backend/Sources/Program.cs @@ -22,7 +22,7 @@ private static void Main(string[] args) .AddDbContext() .AddProblemDetails() .AddAutoMapper(config => config.AddProfile()) - .AddOpenApiDocument() + .AddOpenApiDocument(options => options.Title = "Citizen Proposal App") .AddSingleton(TimeProvider.System) .AddControllers() .AddJsonOptions(options => options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));