Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new UseInlineDefinitionsForObjects flag #2533

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -32,6 +32,7 @@ private void DeepCopy(SchemaGeneratorOptions source, SchemaGeneratorOptions targ
{
target.CustomTypeMappings = new Dictionary<Type, Func<OpenApiSchema>>(source.CustomTypeMappings);
target.UseInlineDefinitionsForEnums = source.UseInlineDefinitionsForEnums;
target.UseInlineDefinitionsForObjects = source.UseInlineDefinitionsForObjects;
target.SchemaIdSelector = source.SchemaIdSelector;
target.IgnoreObsoleteProperties = source.IgnoreObsoleteProperties;
target.UseAllOfForInheritance = source.UseAllOfForInheritance;
Expand Down
Expand Up @@ -204,6 +204,14 @@ public static void UseInlineDefinitionsForEnums(this SwaggerGenOptions swaggerGe
swaggerGenOptions.SchemaGeneratorOptions.UseInlineDefinitionsForEnums = true;
}

/// <summary>
/// Generate inline schema definitions (as opposed to referencing a shared definition) for complex objects
/// </summary>
public static void UseInlineDefinitionsForObjects(this SwaggerGenOptions swaggerGenOptions)
{
swaggerGenOptions.SchemaGeneratorOptions.UseInlineDefinitionsForObjects = true;
}

/// <summary>
/// Provide a custom strategy for generating the unique Id's that are used to reference object Schema's
/// </summary>
Expand Down
Expand Up @@ -244,7 +244,7 @@ private OpenApiSchema GenerateConcreteSchema(DataContract dataContract, SchemaRe
case DataType.Object:
{
schemaFactory = () => CreateObjectSchema(dataContract, schemaRepository);
returnAsReference = true;
returnAsReference = !_generatorOptions.UseInlineDefinitionsForObjects;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the original PR for this change #2426, you'll want to add support for self-referencing types:

returnAsReference = !_generatorOptions.UseInlineDefinitionsForObjects ||
                                        dataContract.ObjectProperties.Any(prop =>
                                            prop.MemberType.IsAssignableFrom(dataContract.UnderlyingType) ||
                                            prop.MemberType.GenericTypeArguments.Any(type =>
                                                type.IsAssignableFrom(dataContract.UnderlyingType)));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test for this to also validate the change:

        [Fact]
        public void GenerateSchema_SupportsOption_UseReferenceForSelfReferenceTypesWhenUseInlineDefinitionsForObjects()
        {
            var subject = Subject(
                configureGenerator: c => c.UseInlineDefinitionsForObjects = true
            );

            var schema = subject.GenerateSchema(typeof(SelfReferencingType), new SchemaRepository());

            Assert.NotNull(schema.Reference);
            Assert.Equal("SelfReferencingType", schema.Reference.Id);
        }

break;
}

Expand Down
Expand Up @@ -21,6 +21,8 @@ public SchemaGeneratorOptions()

public bool UseInlineDefinitionsForEnums { get; set; }

public bool UseInlineDefinitionsForObjects { get; set; }

public Func<Type, string> SchemaIdSelector { get; set; }

public bool IgnoreObsoleteProperties { get; set; }
Expand Down
Expand Up @@ -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()
{
Expand Down
Expand Up @@ -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)]
Expand Down