Skip to content

Commit

Permalink
Fixes UI view (#3561)
Browse files Browse the repository at this point in the history
* Revert "Bumping up HealthcareSharedPackageVersion to the latest."

* Fixes UI view

* .Removing a call to AddRazorRuntimeCompilation in AddFhirServer.

* Update src/Microsoft.Health.Fhir.Shared.Api/Modules/FeatureFlags/HtmlUi/HtmlUiFeatureModule.cs

---------

Co-authored-by: Isao Yamauchi <[email protected]>
  • Loading branch information
brendankowitz and v-iyamauchi authored Oct 23, 2023
1 parent 63de320 commit a3bb92b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -67,58 +68,58 @@ public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext co
EnsureArg.IsNotNull(context, nameof(context));
EnsureArg.IsNotNull(selectedEncoding, nameof(selectedEncoding));

var engine = context.HttpContext.RequestServices.GetService<IRazorViewEngine>();
var tempDataProvider = context.HttpContext.RequestServices.GetService<ITempDataProvider>();
IRazorViewEngine engine = context.HttpContext.RequestServices.GetService<IRazorViewEngine>();
ITempDataProvider tempDataProvider = context.HttpContext.RequestServices.GetService<ITempDataProvider>();

context.HttpContext.AllowSynchronousIO();

var actionContext = new ActionContext(context.HttpContext, context.HttpContext.GetRouteData(), new ActionDescriptor());

using (TextWriter textWriter = context.WriterFactory(context.HttpContext.Response.Body, selectedEncoding))
{
var viewName = "ViewJson";
var viewResult = engine.FindView(actionContext, viewName, true);
await using TextWriter textWriter = context.WriterFactory(context.HttpContext.Response.Body, selectedEncoding);
var viewName = "ViewJson";
ViewEngineResult viewResult = engine.FindView(actionContext, viewName, true);

if (viewResult.View == null)
{
throw new FileNotFoundException(Api.Resources.ViewNotFound, $"{viewName}.cshtml");
}
if (viewResult.View == null)
{
throw new FileNotFoundException(Api.Resources.ViewNotFound, $"{viewName}.cshtml");
}

var resourceInstance = (Resource)context.Object;
string div = null;
var resourceInstance = (Resource)context.Object;
string div = null;

if (resourceInstance is DomainResource domainResourceInstance && !string.IsNullOrEmpty(domainResourceInstance.Text?.Div))
{
div = _htmlSanitizer.Sanitize(domainResourceInstance.Text.Div);
}
if (resourceInstance is DomainResource domainResourceInstance && !string.IsNullOrEmpty(domainResourceInstance.Text?.Div))
{
div = _htmlSanitizer.Sanitize(domainResourceInstance.Text.Div);
}

var stringBuilder = new StringBuilder();
using (var stringWriter = new StringWriter(stringBuilder))
using (var jsonTextWriter = new JsonTextWriter(stringWriter))
{
jsonTextWriter.ArrayPool = _charPool;
jsonTextWriter.Formatting = Formatting.Indented;
var stringBuilder = new StringBuilder();
await using (var stringWriter = new StringWriter(stringBuilder))
await using (var jsonTextWriter = new JsonTextWriter(stringWriter))
{
jsonTextWriter.ArrayPool = _charPool;
jsonTextWriter.Formatting = Formatting.Indented;

await _fhirJsonSerializer.SerializeAsync(resourceInstance, jsonTextWriter, context.HttpContext.GetSummaryTypeOrDefault(), context.HttpContext.GetElementsOrDefault());
}
await _fhirJsonSerializer.SerializeAsync(resourceInstance, jsonTextWriter, context.HttpContext.GetSummaryTypeOrDefault(), context.HttpContext.GetElementsOrDefault());
}

var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
{
Model = new CodePreviewModel
{
Model = new CodePreviewModel
{
Code = stringBuilder.ToString(),
Div = div,
},
};

var viewContext = new ViewContext(
actionContext,
viewResult.View,
viewDictionary,
new TempDataDictionary(actionContext.HttpContext, tempDataProvider),
textWriter,
new HtmlHelperOptions());

await viewResult.View.RenderAsync(viewContext);
}
Code = stringBuilder.ToString(),
Div = div,
},
};

var viewContext = new ViewContext(
actionContext,
viewResult.View,
viewDictionary,
new TempDataDictionary(actionContext.HttpContext, tempDataProvider),
textWriter,
new HtmlHelperOptions());

await viewResult.View.RenderAsync(viewContext);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@

using EnsureThat;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Health.Extensions.DependencyInjection;
using Microsoft.Health.Fhir.Api.Configs;
using Microsoft.Health.Fhir.Api.Features.Formatters;
using Microsoft.Health.Fhir.Api.Models;

namespace Microsoft.Health.Fhir.Api.Modules.FeatureFlags.HtmlUi
{
Expand Down Expand Up @@ -36,6 +39,12 @@ public void Load(IServiceCollection services)
.Singleton()
.AsSelf()
.AsService<TextOutputFormatter>();

// Adds provider to serve embedded razor views
services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
{
options.FileProviders.Add(new EmbeddedFileProvider(typeof(CodePreviewModel).Assembly));
}).AddMvc().AddRazorRuntimeCompilation();
}
}
}
Expand Down
16 changes: 0 additions & 16 deletions src/Microsoft.Health.Fhir.Shared.Api/Modules/MvcModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,19 @@
using EnsureThat;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.FileProviders;
using Microsoft.Health.Extensions.DependencyInjection;
using Microsoft.Health.Fhir.Api.Features.Binders;
using Microsoft.Health.Fhir.Api.Features.Filters;
using Microsoft.Health.Fhir.Api.Features.Routing;
using Microsoft.Health.Fhir.Api.Models;
using Microsoft.Health.Fhir.Core.Features.Routing;

namespace Microsoft.Health.Fhir.Api.Modules
{
public class MvcModule : IStartupModule
{
private readonly EmbeddedFileProvider _embeddedFileProvider;

public MvcModule()
{
_embeddedFileProvider = new EmbeddedFileProvider(typeof(CodePreviewModel).Assembly);
}

/// <inheritdoc />
public void Load(IServiceCollection services)
{
Expand All @@ -42,12 +32,6 @@ public void Load(IServiceCollection services)
options.ConstraintMap.Add(KnownRoutes.CompartmentResourceTypeRouteConstraint, typeof(CompartmentResourceTypesRouteConstraint));
});

// Adds provider to serve embedded razor views
services.Configure<MvcRazorRuntimeCompilationOptions>(options =>
{
options.FileProviders.Add(_embeddedFileProvider);
});

services.PostConfigure<MvcOptions>(options =>
{
options.ModelBinderProviders.Insert(0, new PartialDateTimeBinderProvider());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public static IFhirServerBuilder AddFhirServer(
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.DateParseHandling = Newtonsoft.Json.DateParseHandling.DateTimeOffset;
})
.AddRazorRuntimeCompilation();
});

var fhirServerConfiguration = new FhirServerConfiguration();

Expand Down

0 comments on commit a3bb92b

Please sign in to comment.