Skip to content

Commit

Permalink
Handle JArray's correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
domaindrivendev committed May 16, 2019
1 parent aee70c7 commit 6f9e899
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
Expand Up @@ -85,7 +85,7 @@ public OpenApiSchema GenerateSchema(Type type, SchemaRepository schemaRepository
if (Next != null)
return Next.GenerateSchema(type, schemaRepository);

throw new InvalidOperationException("TODO:");
throw new InvalidOperationException($"Unable to generate schema for type - {type}");
}

protected IContractResolver ContractResolver { get; }
Expand Down
Expand Up @@ -29,9 +29,10 @@ protected override OpenApiSchema GenerateSchemaFor(Type type, SchemaRepository s

private static Dictionary<Type, Func<OpenApiSchema>> KnownTypeMappings = new Dictionary<Type, Func<OpenApiSchema>>
{
{ typeof(object), () => new OpenApiSchema { Type = "object" } },
{ typeof(JToken), () => new OpenApiSchema { Type = "object" } },
{ typeof(JObject), () => new OpenApiSchema { Type = "object" } }
[ typeof(object) ] = () => new OpenApiSchema { Type = "object" },
[ typeof(JToken) ] = () => new OpenApiSchema { Type = "object" },
[ typeof(JObject) ] = () => new OpenApiSchema { Type = "object" },
[ typeof(JArray) ] = () => new OpenApiSchema { Type = "array", Items = new OpenApiSchema { Type = "object" } }
};
}
}
Expand Up @@ -16,14 +16,17 @@ namespace Swashbuckle.AspNetCore.SwaggerGen.Test
public class SchemaGeneratorTests
{
[Theory]
[InlineData(typeof(object))]
[InlineData(typeof(JToken))]
[InlineData(typeof(JObject))]
public void GenerateSchema_GeneratesDynamicSchema_IfDynamicType(Type type)
[InlineData(typeof(object), "object")]
[InlineData(typeof(JToken), "object")]
[InlineData(typeof(JObject), "object")]
[InlineData(typeof(JArray), "array")]
public void GenerateSchema_GeneratesDynamicSchema_IfDynamicType(
Type type,
string expectedType)
{
var schema = Subject().GenerateSchema(type, new SchemaRepository());

Assert.Equal("object", schema.Type);
Assert.Equal(expectedType, schema.Type);
Assert.Empty(schema.Properties);
}

Expand Down
6 changes: 6 additions & 0 deletions test/WebSites/Basic/Controllers/DynamicTypesController.cs
Expand Up @@ -35,5 +35,11 @@ public IEnumerable<JObject> GetWizards()
new JObject()
};
}

[HttpGet("witches")]
public JArray GetWitches()
{
return JArray.FromObject(new[] { "foo", "bar" });
}
}
}

0 comments on commit 6f9e899

Please sign in to comment.