Skip to content

Commit

Permalink
Support new UseInlineDefinitionsForObjects flag
Browse files Browse the repository at this point in the history
  • Loading branch information
sol-wasserman committed Nov 2, 2022
1 parent 4a28652 commit 318d3cc
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private OpenApiSchema GenerateConcreteSchema(DataContract dataContract, SchemaRe
case DataType.Object:
{
schemaFactory = () => CreateObjectSchema(dataContract, schemaRepository);
returnAsReference = true;
returnAsReference = !_generatorOptions.UseInlineDefinitionsForObjects;
break;
}

Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 318d3cc

Please sign in to comment.