Skip to content

Commit

Permalink
Add support for any kind of example object when schema is string type…
Browse files Browse the repository at this point in the history
… preferring the serialized result. Fixes domaindrivendev#2088
  • Loading branch information
Havunen committed Feb 18, 2024
1 parent 2d2d151 commit 8e3861d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,26 @@ SchemaRepository schemaRepository
)
{
var resolvedType = schema?.ResolveType(schemaRepository);
var shouldBeQuoted = (
var isStringSchema = (
string.Equals(resolvedType, "string", StringComparison.Ordinal) ||
string.Equals(resolvedType, "array", StringComparison.Ordinal)
) && !string.Equals(example, "null", StringComparison.Ordinal);
var exampleValue = WhiteSpaceCleaner.Condense(example);

return OpenApiAnyFactory.CreateFromJson(shouldBeQuoted ? $"\"{exampleValue}\"" : exampleValue);

var serializedSchema = OpenApiAnyFactory.CreateFromJson(exampleValue);

if (serializedSchema != null)
{
return serializedSchema;
}

if (isStringSchema)
{
return new OpenApiString(exampleValue);
}

return null;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using DotSwashbuckle.AspNetCore.TestSupport;

namespace DotSwashbuckle.AspNetCore.SwaggerGen.Test
Expand Down Expand Up @@ -80,6 +81,22 @@ public class XmlAnnotatedType
/// </example>
public string MultiLineSummaryExample { get; set; }

/// <sumary>
/// For test only
/// </sumary>
/// <value>For test only</value>
/// <example>&quot;Test-Test-Test&quot;</example>
[DataMember(Name = "testProperty", EmitDefaultValue = false)]
public string QuoteProperty { get; set; }

/// <sumary>
/// For test only
/// </sumary>
/// <value>For test only</value>
/// <example>{"someKey": "someValue"}</example>
[DataMember(Name = "testProperty", EmitDefaultValue = false)]
public string KvpExampleProperty { get; set; }

/// <summary>
/// Summary for StringPropertyWithUri
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public void Apply_SetsDescription_FromPropertySummaryTag(
[InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringPropertyWithNullExample), "string", "null")]
[InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.StringPropertyWithUri), "string", "\"https://test.com/a?b=1&c=2\"")]
[InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.MultiLineSummaryExample), "string", "\"When Number equals 2023/S 106-333284 Then Publication = 333284-2023\"")]
[InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.QuoteProperty), "string", "\"Test-Test-Test\"")]
[InlineData(typeof(XmlAnnotatedType), nameof(XmlAnnotatedType.KvpExampleProperty), "string", "{\n \"someKey\": \"someValue\"\n}")]
[InlineData(typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.BoolProperty), "boolean", "true")]
[InlineData(typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.IntProperty), "integer", "10")]
[InlineData(typeof(XmlAnnotatedRecord), nameof(XmlAnnotatedRecord.LongProperty), "integer", "4294967295")]
Expand Down

0 comments on commit 8e3861d

Please sign in to comment.