Skip to content

Commit

Permalink
fix: review
Browse files Browse the repository at this point in the history
  • Loading branch information
BigDaddy1337 committed Oct 9, 2024
1 parent c6a484f commit 1b31871
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/Tochka.JsonRpc.OpenRpc/Models/XmlDocValuesWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Tochka.JsonRpc.OpenRpc.Models;

internal record XmlDocValuesWrapper(string? Summary, string? Remarks);
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
using Json.Schema;
using Tochka.JsonRpc.OpenRpc.Models;

namespace Tochka.JsonRpc.OpenRpc.Services;

internal record XmlDocValues(string? Summary, string? Remarks);

internal static class JsonSchemaBuilderExtensions
{
public static JsonSchemaBuilder AppendXmlDocs(this JsonSchemaBuilder builder, XmlDocValues xmlDocs)
private const string OpenRpcUiNewLine = "\n\r";

public static JsonSchemaBuilder AppendXmlDocs(this JsonSchemaBuilder builder, XmlDocValuesWrapper xmlDocs)
{
const string openRpcUiNewLine = "\n\r";

var documentation = xmlDocs.Summary;

if (!string.IsNullOrEmpty(xmlDocs.Remarks))
{
if (!string.IsNullOrEmpty(documentation))
{
documentation += openRpcUiNewLine;
documentation += OpenRpcUiNewLine;
}

documentation += xmlDocs.Remarks;
Expand All @@ -25,9 +24,9 @@ public static JsonSchemaBuilder AppendXmlDocs(this JsonSchemaBuilder builder, Xm
if (!string.IsNullOrEmpty(documentation))
{
// OpenRPC UI does not accept line breaks without carriage return
documentation = documentation.Replace(Environment.NewLine, openRpcUiNewLine);
documentation = documentation.Replace(Environment.NewLine, OpenRpcUiNewLine);

var newLineIndex = documentation.IndexOf(openRpcUiNewLine, StringComparison.Ordinal);
var newLineIndex = documentation.IndexOf(OpenRpcUiNewLine, StringComparison.Ordinal);
if (newLineIndex >= 0)
{
builder.Title(documentation[..newLineIndex] + "...")
Expand Down
9 changes: 5 additions & 4 deletions src/Tochka.JsonRpc.OpenRpc/Services/OpenRpcSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Json.Schema.Generation;
using Namotion.Reflection;
using Tochka.JsonRpc.Common;
using Tochka.JsonRpc.OpenRpc.Models;

namespace Tochka.JsonRpc.OpenRpc.Services;

Expand Down Expand Up @@ -46,7 +47,7 @@ private JsonSchema CreateOrRefInternal(Type type, string methodName, PropertyInf

private JsonSchema BuildSchema(Type type, string typeName, string methodName, PropertyInfo? property, JsonSerializerOptions jsonSerializerOptions)
{
var propertyXmlDocs = new XmlDocValues(property?.GetXmlDocsSummary(), property?.GetXmlDocsRemarks());
var propertyXmlDocs = new XmlDocValuesWrapper(property?.GetXmlDocsSummary(), property?.GetXmlDocsRemarks());

if (registeredSchemas.ContainsKey(typeName) || registeredSchemaKeys.Contains(typeName))
{
Expand Down Expand Up @@ -75,7 +76,7 @@ private JsonSchema BuildSchema(Type type, string typeName, string methodName, Pr
}
var enumSchema = new JsonSchemaBuilder()
.Enum(enumValues)
.AppendXmlDocs(new XmlDocValues(type.GetXmlDocsSummary(), type.GetXmlDocsRemarks()))
.AppendXmlDocs(new XmlDocValuesWrapper(type.GetXmlDocsSummary(), type.GetXmlDocsRemarks()))
.BuildWithoutUri();
RegisterSchema(typeName, enumSchema);
// returning ref if it's enum or regular type with properties
Expand Down Expand Up @@ -113,7 +114,7 @@ private JsonSchema BuildSchema(Type type, string typeName, string methodName, Pr
var jsonSchemaBuilder = new JsonSchemaBuilder()
.Type(SchemaValueType.Object)
.Properties(propertiesSchemas)
.AppendXmlDocs(new XmlDocValues(type.GetXmlDocsSummary(), type.GetXmlDocsRemarks()));
.AppendXmlDocs(new XmlDocValuesWrapper(type.GetXmlDocsSummary(), type.GetXmlDocsRemarks()));

if (requiredProperties is not null)
{
Expand Down Expand Up @@ -244,7 +245,7 @@ private static string GetClearTypeName(string methodName, Type clearType)
return null;
}

private static JsonSchema CreateRefSchema(string typeName, XmlDocValues propertyXmlDocs)
private static JsonSchema CreateRefSchema(string typeName, XmlDocValuesWrapper propertyXmlDocs)
{
var refSchemaBuilder = new JsonSchemaBuilder()
.Ref($"#/components/schemas/{typeName}")
Expand Down

0 comments on commit 1b31871

Please sign in to comment.