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 455a3d2947..c5560531a6 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 1ef837d221..96d5f40327 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 6f2d1e7227..589d7ce169 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs @@ -564,6 +564,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)]