Skip to content

Commit

Permalink
Add option to return pretanslations as USFM
Browse files Browse the repository at this point in the history
  • Loading branch information
ddaspit committed Jan 18, 2024
1 parent 04f977f commit 8f1e06d
Show file tree
Hide file tree
Showing 38 changed files with 1,289 additions and 656 deletions.
6 changes: 3 additions & 3 deletions src/Serval.ApiServer/Serval.ApiServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<PackageReference Include="Hangfire" Version="1.7.33" />
<PackageReference Include="Hangfire.Mongo" Version="1.9.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.14" />
<PackageReference Include="NSwag.AspNetCore" Version="13.20.0" />
<PackageReference Include="NSwag.MSBuild" Version="13.20.0">
<PackageReference Include="NSwag.AspNetCore" Version="14.0.2" />
<PackageReference Include="NSwag.MSBuild" Version="14.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down Expand Up @@ -50,6 +50,6 @@
</PropertyGroup>

<Target Name="NSwag" AfterTargets="PostBuildEvent" Condition=" '$(Configuration)' == 'Debug' ">
<Exec WorkingDirectory="$(ProjectDir)" EnvironmentVariables="ASPNETCORE_ENVIRONMENT=Development;NSWAG=1" Command="$(NSwagExe_Net60) run nswag.json /variables:Configuration=$(Configuration)" />
<Exec WorkingDirectory="$(ProjectDir)" EnvironmentVariables="ASPNETCORE_ENVIRONMENT=Development;NSWAG=1" Command="$(NSwagExe_Net80) run nswag.json /variables:Configuration=$(Configuration)" />
</Target>
</Project>
8 changes: 4 additions & 4 deletions src/Serval.ApiServer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerDocument(o =>
{
o.SchemaType = SchemaType.Swagger2;
o.SchemaSettings.SchemaType = SchemaType.Swagger2;
o.Title = "Serval API";
o.Description = "Natural language processing services for minority language Bible translation.";
o.DocumentName = "v" + version.Major;
o.ApiGroupNames = new[] { "v" + version.Major };
o.Version = version.Major + "." + version.Minor;

o.SchemaNameGenerator = new ServalSchemaNameGenerator();
o.SchemaSettings.SchemaNameGenerator = new ServalSchemaNameGenerator();
o.UseControllerSummaryAsTagDescription = true;
o.AddSecurity(
"bearer",
Expand All @@ -165,7 +165,7 @@ public void ConfigureServices(IServiceCollection services)
);
o.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("bearer"));

o.AllowReferencesWithProperties = true;
o.SchemaSettings.AllowReferencesWithProperties = true;
o.PostProcess = document =>
{
var prefix = "/api/v" + version.Major;
Expand Down Expand Up @@ -223,7 +223,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
document.Servers.First().Url += prefix;
};
});
app.UseSwaggerUi3(settings =>
app.UseSwaggerUi(settings =>
{
settings.OAuth2Client = new OAuth2ClientSettings
{
Expand Down
72 changes: 72 additions & 0 deletions src/Serval.ApiServer/Templates/Client.Class.ProcessResponse.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{% if response.HasType -%}
{% if response.IsFile -%}
{% if response.IsSuccess -%}
var responseStream_ = response_.Content == null ? System.IO.Stream.Null : await response_.Content.ReadAsStreamAsync().ConfigureAwait(false);
var fileResponse_ = new FileResponse(status_, headers_, responseStream_, {% if InjectHttpClient or DisposeHttpClient == false %}null{% else %}client_{% endif %}, response_);
disposeClient_ = false; disposeResponse_ = false; // response and client are disposed by FileResponse
return fileResponse_;
{% else -%}
var objectResponse_ = await ReadObjectResponseAsync<{{ response.Type }}>(response_, headers_, cancellationToken).ConfigureAwait(false);
throw new {{ ExceptionClass }}<{{ response.Type }}>("{{ response.ExceptionDescription }}", status_, objectResponse_.Text, headers_, objectResponse_.Object, null);
{% endif -%}
{% elsif response.IsPlainText or operation.Produces == "text/plain" -%}
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
var result_ = ({{ response.Type }})System.Convert.ChangeType(responseData_, typeof({{ response.Type }}));
{% if response.IsSuccess -%}
{% if operation.WrapResponse -%}
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, result_);
{% else -%}
return result_;
{% endif -%}
{% else -%}
throw new {{ ExceptionClass }}<{{ response.Type }}>("{{ response.ExceptionDescription }}", status_, responseData_, headers_, result_, null);
{% endif -%}
{% else -%}
var objectResponse_ = await ReadObjectResponseAsync<{{ response.Type }}>(response_, headers_, cancellationToken).ConfigureAwait(false);
{% if response.IsNullable == false -%}
if (objectResponse_.Object == null)
{
throw new {{ ExceptionClass }}("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
}
{% endif -%}
{% if response.IsSuccess -%}
{% if operation.WrapResponse -%}
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, objectResponse_.Object);
{% else -%}
return objectResponse_.Object;
{% endif -%}
{% endif -%}
{% if response.IsSuccess == false -%}
{% if response.InheritsExceptionSchema -%}
var responseObject_ = objectResponse_.Object != null ? objectResponse_.Object : new {{ response.Type }}();
responseObject_.Data.Add("HttpStatus", status_.ToString());
responseObject_.Data.Add("HttpHeaders", headers_);
responseObject_.Data.Add("HttpResponse", objectResponse_.Text);
{% if WrapDtoExceptions -%}
throw new {{ ExceptionClass }}("{{ response.ExceptionDescription }}", status_, objectResponse_.Text, headers_, responseObject_);
{% else -%}
throw responseObject_;
{% endif -%}
{% else -%}
throw new {{ ExceptionClass }}<{{ response.Type }}>("{{ response.ExceptionDescription }}", status_, objectResponse_.Text, headers_, objectResponse_.Object, null);
{% endif -%}
{% endif -%}
{% endif -%}
{% elsif response.IsSuccess -%}
{% if operation.HasResultType -%}
{% if operation.WrapResponse -%}
return new {{ ResponseClass }}<{{ operation.UnwrappedResultType }}>(status_, headers_, {{ operation.UnwrappedResultDefaultValue }});
{% else -%}
return {{ operation.UnwrappedResultDefaultValue }};
{% endif -%}
{% else -%}
{% if operation.WrapResponse -%}
return new {{ ResponseClass }}(status_, headers_);
{% else -%}
return;
{% endif -%}
{% endif -%}
{% else -%}{% comment %} implied: `if !response.HasType` so just read it as text {% endcomment %}
string responseText_ = ( response_.Content == null ) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new {{ ExceptionClass }}("{{ response.ExceptionDescription }}", status_, responseText_, headers_, null);
{% endif %}
57 changes: 8 additions & 49 deletions src/Serval.ApiServer/nswag.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"runtime": "Net60",
"runtime": "Net80",
"defaultVariables": null,
"documentGenerator": {
"aspNetCoreToOpenApi": {
"project": "Serval.ApiServer.csproj",
"documentName": "v1",
"msBuildProjectExtensionsPath": null,
"configuration": null,
"runtime": null,
Expand All @@ -12,60 +13,19 @@
"msBuildOutputPath": null,
"verbose": true,
"workingDirectory": null,
"requireParametersWithoutDefault": false,
"apiGroupNames": null,
"defaultPropertyNameHandling": "Default",
"defaultReferenceTypeNullHandling": "Null",
"defaultDictionaryValueReferenceTypeNullHandling": "NotNull",
"defaultResponseReferenceTypeNullHandling": "NotNull",
"generateOriginalParameterNames": true,
"defaultEnumHandling": "Integer",
"flattenInheritanceHierarchy": false,
"generateKnownTypes": true,
"generateEnumMappingDescription": false,
"generateXmlObjects": false,
"generateAbstractProperties": false,
"generateAbstractSchemas": true,
"ignoreObsoleteProperties": false,
"allowReferencesWithProperties": false,
"useXmlDocumentation": true,
"resolveExternalXmlDocumentation": true,
"excludedTypeNames": [],
"serviceHost": null,
"serviceBasePath": null,
"serviceSchemes": [],
"infoTitle": "My Title",
"infoDescription": null,
"infoVersion": "1.0.0",
"documentTemplate": null,
"documentProcessorTypes": [],
"operationProcessorTypes": [],
"typeNameGeneratorType": null,
"schemaNameGeneratorType": null,
"contractResolverType": null,
"serializerSettingsType": null,
"useDocumentProvider": true,
"documentName": "v1",
"aspNetCoreEnvironment": "Development",
"createWebHostBuilderMethod": null,
"startupType": null,
"allowNullableBodyParameters": true,
"useHttpAttributeNameAsOperationId": false,
"output": null,
"outputType": "Swagger2",
"newLineBehavior": "Auto",
"assemblyPaths": [],
"assemblyConfig": null,
"referencePaths": [],
"useNuGetCache": false
"newLineBehavior": "Auto"
}
},
"codeGenerators": {
"openApiToCSharpClient": {
"clientBaseClass": null,
"configurationClass": null,
"generateClientClasses": true,
"suppressClientClassesOutput": false,
"generateClientInterfaces": true,
"suppressClientInterfacesOutput": false,
"clientBaseInterface": null,
"injectHttpClient": true,
"disposeHttpClient": true,
Expand All @@ -83,6 +43,8 @@
"exposeJsonSerializerSettings": false,
"clientClassAccessModifier": "public",
"typeAccessModifier": "public",
"propertySetterAccessModifier": "",
"generateNativeRecords": false,
"generateContractsOutput": false,
"contractsNamespace": null,
"contractsOutputFilePath": null,
Expand Down Expand Up @@ -138,10 +100,7 @@
"generateDtoTypes": true,
"generateOptionalPropertiesAsNullable": false,
"generateNullableReferenceTypes": true,
"templateDirectory": null,
"typeNameGeneratorType": null,
"propertyNameGeneratorType": null,
"enumNameGeneratorType": null,
"templateDirectory": "Templates",
"serviceHost": null,
"serviceSchemes": null,
"output": "../Serval.Client/Client.g.cs",
Expand Down
Loading

0 comments on commit 8f1e06d

Please sign in to comment.