Skip to content

Commit

Permalink
(#771) Tests to show read-only controllers work now. (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianhall committed Oct 6, 2023
1 parent 8bff35a commit 7693ea9
Show file tree
Hide file tree
Showing 7 changed files with 471 additions and 12 deletions.
Expand Up @@ -42,8 +42,7 @@ private static void ProcessDatasyncOperation(OperationProcessorContext context)
var path = context.OperationDescription.Path;
Type entityType = context.ControllerType.BaseType?.GetGenericArguments().FirstOrDefault()
?? throw new ArgumentException("Cannot process a non-generic table controller");
var entitySchema = context.SchemaResolver.GetSchema(entityType, false);
var entitySchemaRef = new JsonSchema { Reference = entitySchema };
JsonSchema entitySchemaRef = GetEntityReference(context, entityType);

operation.AddDatasyncRequestHeaders();
if (method.Equals("DELETE", StringComparison.InvariantCultureIgnoreCase))
Expand Down Expand Up @@ -94,6 +93,25 @@ private static void ProcessDatasyncOperation(OperationProcessorContext context)
}
}

/// <summary>
/// Either reads or generates the required entity type schema.
/// </summary>
/// <param name="context">The context for the operation processor.</param>
/// <param name="entityType">The entity type needed.</param>
/// <returns>A reference to the entity schema.</returns>
private static JsonSchema GetEntityReference(OperationProcessorContext context, Type entityType)
{
var schemaName = context.SchemaGenerator.Settings.SchemaNameGenerator.Generate(entityType);
if (!context.Document.Definitions.ContainsKey(schemaName))
{
var newSchema = context.SchemaGenerator.Generate(entityType);
context.Document.Definitions.Add(schemaName, newSchema);
}

var actualSchema = context.Document.Definitions[schemaName];
return new JsonSchema { Reference = actualSchema };
}

/// <summary>
/// Creates the paged item schema reference.
/// </summary>
Expand Down
Expand Up @@ -16,14 +16,14 @@ private static string ReadExternalFile(string filename)
{
Assembly asm = Assembly.GetExecutingAssembly();
using Stream s = asm.GetManifestResourceStream(asm.GetName().Name + "." + filename)!;
using StreamReader sr = new StreamReader(s);
using StreamReader sr = new(s);
return sr.ReadToEnd();
}

private static void WriteExternalFile(string filename, string content)
{
var storePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
using StreamWriter outputFile = new StreamWriter(Path.Combine(storePath, filename));
using StreamWriter outputFile = new(Path.Combine(storePath, filename));
outputFile.Write(content);
}

Expand All @@ -40,6 +40,6 @@ public async Task NSwag_GeneratesSwagger()
{
WriteExternalFile("swagger.json.out", actualContent);
}
Assert.Equal(expectedContent, actualContent.Replace("\r\n", "\n").TrimEnd());
Assert.Equal(expectedContent, actualContent);
}
}
@@ -0,0 +1,45 @@
using Microsoft.AspNetCore.Datasync.EFCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNetCore.Datasync.NSwag.Test.Service;

[ExcludeFromCodeCoverage]
public abstract class ReadonlyTableController<TData> : TableController<TData> where TData : class, ITableData
{
[NonAction]
public override Task<IActionResult> CreateAsync([FromBody] TData item, CancellationToken token = default)
{
return base.CreateAsync(item, token);
}

[NonAction]
public override Task<IActionResult> DeleteAsync([FromRoute] string id, CancellationToken token = default)
{
return base.DeleteAsync(id, token);
}

[NonAction]
public override Task<IActionResult> PatchAsync([FromRoute] string id, CancellationToken token = default)
{
return base.PatchAsync(id, token);
}

[NonAction]
public override Task<IActionResult> ReplaceAsync([FromRoute] string id, [FromBody] TData item, CancellationToken token = default)
{
return base.ReplaceAsync(id, item, token);
}
}

[Route("tables/kitchenreader")]
[ExcludeFromCodeCoverage]
public class KitchenReaderController : ReadonlyTableController<KitchenSink>
{
public KitchenReaderController(ServiceDbContext context, ILogger<KitchenSink> logger) : base()
{
Repository = new EntityTableRepository<KitchenSink>(context);
Logger = logger;
}
}

0 comments on commit 7693ea9

Please sign in to comment.