From 3f8b012ab5ab8edc7b56f42b94092bb56c7d70a1 Mon Sep 17 00:00:00 2001 From: sol-wasserman Date: Wed, 23 Mar 2022 18:06:58 -0400 Subject: [PATCH] Support new `UseInlineDefinitionsForObjects` flag --- .../ConfigureSchemaGeneratorOptions.cs | 1 + .../SwaggerGenOptionsExtensions.cs | 8 ++++++++ .../SchemaGenerator/SchemaGenerator.cs | 2 +- .../SchemaGenerator/SchemaGeneratorOptions.cs | 2 ++ .../NewtonsoftSchemaGeneratorTests.cs | 13 +++++++++++++ .../JsonSerializerSchemaGeneratorTests.cs | 13 +++++++++++++ 6 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/ConfigureSchemaGeneratorOptions.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/ConfigureSchemaGeneratorOptions.cs index 565811584f..8cfeee9f42 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/ConfigureSchemaGeneratorOptions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/ConfigureSchemaGeneratorOptions.cs @@ -32,6 +32,7 @@ private void DeepCopy(SchemaGeneratorOptions source, SchemaGeneratorOptions targ { target.CustomTypeMappings = new Dictionary>(source.CustomTypeMappings); target.UseInlineDefinitionsForEnums = source.UseInlineDefinitionsForEnums; + target.UseInlineDefinitionsForObjects = source.UseInlineDefinitionsForObjects; target.SchemaIdSelector = source.SchemaIdSelector; target.IgnoreObsoleteProperties = source.IgnoreObsoleteProperties; target.UseAllOfForInheritance = source.UseAllOfForInheritance; diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/SwaggerGenOptionsExtensions.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/SwaggerGenOptionsExtensions.cs index 2239084555..1113c370fe 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/SwaggerGenOptionsExtensions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/SwaggerGenOptionsExtensions.cs @@ -204,6 +204,14 @@ public static void UseInlineDefinitionsForEnums(this SwaggerGenOptions swaggerGe swaggerGenOptions.SchemaGeneratorOptions.UseInlineDefinitionsForEnums = true; } + /// + /// Generate inline schema definitions (as opposed to referencing a shared definition) for complex objects + /// + public static void UseInlineDefinitionsForObjects(this SwaggerGenOptions swaggerGenOptions) + { + swaggerGenOptions.SchemaGeneratorOptions.UseInlineDefinitionsForObjects = true; + } + /// /// Provide a custom strategy for generating the unique Id's that are used to reference object Schema's /// diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs index de2ac497fc..b75c431643 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs @@ -244,7 +244,7 @@ private OpenApiSchema GenerateConcreteSchema(DataContract dataContract, SchemaRe case DataType.Object: { schemaFactory = () => CreateObjectSchema(dataContract, schemaRepository); - returnAsReference = true; + returnAsReference = !_generatorOptions.UseInlineDefinitionsForObjects; break; } diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGeneratorOptions.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGeneratorOptions.cs index 1fccd6ec84..e2548fdf47 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGeneratorOptions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGeneratorOptions.cs @@ -21,6 +21,8 @@ public SchemaGeneratorOptions() public bool UseInlineDefinitionsForEnums { get; set; } + public bool UseInlineDefinitionsForObjects { get; set; } + public Func SchemaIdSelector { get; set; } public bool IgnoreObsoleteProperties { get; set; } diff --git a/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs b/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs index 58b91c440b..ebf6f26da9 100644 --- a/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs +++ b/test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs @@ -533,6 +533,19 @@ public void GenerateSchema_SupportsOption_UseInlineDefinitionsForEnums() Assert.NotNull(schema.Enum); } + [Fact] + public void GenerateSchema_SupportsOption_UseInlineDefinitionsForObjects() + { + var subject = Subject( + configureGenerator: c => c.UseInlineDefinitionsForObjects = true + ); + + var schema = subject.GenerateSchema(typeof(ComplexType), new SchemaRepository()); + + Assert.Null(schema.Reference); + Assert.Equal("object", schema.Type); + } + [Fact] public void GenerateSchema_HandlesTypesWithNestedTypes() { diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs index 30da476cec..a838052b0c 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs @@ -568,6 +568,19 @@ public void GenerateSchema_SupportsOption_UseInlineDefinitionsForEnums() Assert.NotNull(schema.Enum); } + [Fact] + public void GenerateSchema_SupportsOption_UseInlineDefinitionsForObjects() + { + var subject = Subject( + configureGenerator: c => c.UseInlineDefinitionsForObjects = true + ); + + var schema = subject.GenerateSchema(typeof(ComplexType), new SchemaRepository()); + + Assert.Null(schema.Reference); + Assert.Equal("object", schema.Type); + } + [Theory] [InlineData(typeof(TypeWithNullableContext), nameof(TypeWithNullableContext.NullableInt), true)] [InlineData(typeof(TypeWithNullableContext), nameof(TypeWithNullableContext.NonNullableInt), false)]