Skip to content

Commit 00ed420

Browse files
irvinesundayIrvine Sunday
andauthored
Allows schema examples to be optional (#73)
* Adds Links to EntitySet type response objects * Adds links to the test OpenAPI docs. * Refactor to use Utils class for nullability checks * Modify link generator to handle all instances of IEdmEntityType * Update arguments * Add new Link properties * Update test files with links properties * Rename parameter * Fix OpenAPI Link generation * Reorder Parameters generation before Responses This is important so that the parameters info can be used for Links generation * Update test files to validate Link fixes * Fix links and add optional setting * Update test for Links * Revert project PlatformTarget * Add comment * Add setting for enabling/disabling showing of schema examples * Add setting to show schema examples to allow tests to pass * Update test doc. to test disabling of showing of schema examples * Refactor to remove unnecessary Link creation of collection of entities * Revert platform target to default - AnyCPU * Add helpful comment * Grammar nit fix Co-authored-by: Irvine Sunday <[email protected]> Co-authored-by: Irvine Sunday <[email protected]>
1 parent 9d44019 commit 00ed420

File tree

8 files changed

+66
-820
lines changed

8 files changed

+66
-820
lines changed

src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiSchemaGenerator.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,18 @@ private static OpenApiSchema CreateSchemaTypeSchema(this ODataContext context, I
208208
}
209209
}
210210

211-
private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext context, IEdmStructuredType structuredType, bool processBase, bool processExample,
211+
private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext context, IEdmStructuredType structuredType, bool processBase, bool processExample,
212212
IEnumerable<IEdmEntityType> derivedTypes = null)
213213
{
214214
Debug.Assert(context != null);
215215
Debug.Assert(structuredType != null);
216216

217+
IOpenApiAny example = null;
218+
if (context.Settings.ShowSchemaExamples)
219+
{
220+
example = CreateStructuredTypePropertiesExample(context, structuredType);
221+
}
222+
217223
if (context.Settings.EnableDiscriminatorValue && derivedTypes == null)
218224
{
219225
derivedTypes = context.Model.FindDirectlyDerivedTypes(structuredType).OfType<IEdmEntityType>();
@@ -256,7 +262,7 @@ private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext contex
256262
AnyOf = null,
257263
OneOf = null,
258264
Properties = null,
259-
Example = CreateStructuredTypePropertiesExample(context, structuredType)
265+
Example = example
260266
};
261267
}
262268
else
@@ -305,7 +311,7 @@ private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext contex
305311

306312
if (processExample)
307313
{
308-
schema.Example = CreateStructuredTypePropertiesExample(context, structuredType);
314+
schema.Example = example;
309315
}
310316

311317
return schema;

src/Microsoft.OpenApi.OData.Reader/OpenApiConvertSettings.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ public class OpenApiConvertSettings
125125
/// </summary>
126126
public bool ShowLinks { get; set; } = false;
127127

128+
/// <summary>
129+
/// Gets/Sets a value indicating whether or not to show schema examples.
130+
/// </summary>
131+
public bool ShowSchemaExamples { get; set; } = false;
132+
128133
internal OpenApiConvertSettings Clone()
129134
{
130135
var newSettings = new OpenApiConvertSettings
@@ -150,7 +155,8 @@ internal OpenApiConvertSettings Clone()
150155
EnableDerivedTypesReferencesForResponses = this.EnableDerivedTypesReferencesForResponses,
151156
EnableDerivedTypesReferencesForRequestBody = this.EnableDerivedTypesReferencesForRequestBody,
152157
PathPrefix = this.PathPrefix,
153-
ShowLinks = this.ShowLinks
158+
ShowLinks = this.ShowLinks,
159+
ShowSchemaExamples = this.ShowSchemaExamples
154160
};
155161

156162
return newSettings;

test/Microsoft.OpenAPI.OData.Reader.Tests/EdmModelOpenApiExtensionsTest.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,11 @@ public void BasicEdmModelToOpenApiJsonWorks(OpenApiSpecVersion specVersion)
8888
{
8989
// Arrange
9090
IEdmModel model = EdmModelHelper.BasicEdmModel;
91-
var openApiConvertSettings = new OpenApiConvertSettings();
92-
openApiConvertSettings.OpenApiSpecVersion = specVersion;
91+
var openApiConvertSettings = new OpenApiConvertSettings
92+
{
93+
OpenApiSpecVersion = specVersion,
94+
ShowSchemaExamples = true // test for schema examples
95+
};
9396

9497
// Act
9598
string json = WriteEdmModelAsOpenApi(model, OpenApiFormat.Json, openApiConvertSettings);
@@ -113,8 +116,11 @@ public void BasicEdmModelToOpenApiYamlWorks(OpenApiSpecVersion specVersion)
113116
{
114117
// Arrange
115118
IEdmModel model = EdmModelHelper.BasicEdmModel;
116-
var openApiConvertSettings = new OpenApiConvertSettings();
117-
openApiConvertSettings.OpenApiSpecVersion = specVersion;
119+
var openApiConvertSettings = new OpenApiConvertSettings
120+
{
121+
OpenApiSpecVersion = specVersion,
122+
ShowSchemaExamples = true
123+
};
118124

119125
// Act
120126
string yaml = WriteEdmModelAsOpenApi(model, OpenApiFormat.Yaml, openApiConvertSettings);
@@ -138,9 +144,12 @@ public void MultipleSchemasEdmModelToOpenApiJsonWorks(OpenApiSpecVersion specVer
138144
{
139145
// Arrange
140146
IEdmModel model = EdmModelHelper.MultipleSchemasEdmModel;
141-
var openApiConvertSettings = new OpenApiConvertSettings();
142-
openApiConvertSettings.OpenApiSpecVersion = specVersion;
143-
openApiConvertSettings.ShowLinks = true; // test Links
147+
var openApiConvertSettings = new OpenApiConvertSettings
148+
{
149+
OpenApiSpecVersion = specVersion,
150+
ShowLinks = true, // test Links
151+
ShowSchemaExamples = true
152+
};
144153

145154
// Act
146155
string json = WriteEdmModelAsOpenApi(model, OpenApiFormat.Json, openApiConvertSettings);
@@ -164,9 +173,12 @@ public void MultipleSchemasEdmModelToOpenApiYamlWorks(OpenApiSpecVersion specVer
164173
{
165174
// Arrange
166175
IEdmModel model = EdmModelHelper.MultipleSchemasEdmModel;
167-
var openApiConvertSettings = new OpenApiConvertSettings();
168-
openApiConvertSettings.OpenApiSpecVersion = specVersion;
169-
openApiConvertSettings.ShowLinks = true; // test Links
176+
var openApiConvertSettings = new OpenApiConvertSettings
177+
{
178+
OpenApiSpecVersion = specVersion,
179+
ShowLinks = true, // test Links
180+
ShowSchemaExamples = true
181+
};
170182

171183
// Act
172184
string yaml = WriteEdmModelAsOpenApi(model, OpenApiFormat.Yaml, openApiConvertSettings);

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ public void CreateComplexTypeWithoutBaseSchemaReturnCorrectSchema()
6161
{
6262
// Arrange
6363
IEdmModel model = EdmModelHelper.MultipleInheritanceEdmModel;
64-
ODataContext context = new ODataContext(model);
64+
ODataContext context = new ODataContext(model, new OpenApiConvertSettings
65+
{
66+
ShowSchemaExamples = true
67+
});
6568
IEdmComplexType complex = model.SchemaElements.OfType<IEdmComplexType>().First(t => t.Name == "Address");
6669
Assert.NotNull(complex); // Guard
6770

@@ -112,7 +115,8 @@ public void CreateComplexTypeWithBaseSchemaReturnCorrectSchema()
112115
IEdmModel model = EdmModelHelper.MultipleInheritanceEdmModel;
113116
ODataContext context = new ODataContext(model, new OpenApiConvertSettings
114117
{
115-
IEEE754Compatible = true
118+
IEEE754Compatible = true,
119+
ShowSchemaExamples = true
116120
});
117121
IEdmComplexType complex = model.SchemaElements.OfType<IEdmComplexType>().First(t => t.Name == "Tree");
118122
Assert.NotNull(complex); // Guard
@@ -201,7 +205,10 @@ public void CreateEntityTypeWithoutBaseSchemaReturnCorrectSchema()
201205
{
202206
// Arrange
203207
IEdmModel model = EdmModelHelper.MultipleInheritanceEdmModel;
204-
ODataContext context = new ODataContext(model);
208+
ODataContext context = new ODataContext(model, new OpenApiConvertSettings
209+
{
210+
ShowSchemaExamples = true
211+
});
205212
IEdmEntityType entity = model.SchemaElements.OfType<IEdmEntityType>().First(t => t.Name == "Zoo");
206213
Assert.NotNull(entity); // Guard
207214

@@ -258,7 +265,10 @@ public void CreateEntityTypeWithBaseSchemaReturnCorrectSchema()
258265
{
259266
// Arrange
260267
IEdmModel model = EdmModelHelper.MultipleInheritanceEdmModel;
261-
ODataContext context = new ODataContext(model);
268+
ODataContext context = new ODataContext(model, new OpenApiConvertSettings
269+
{
270+
ShowSchemaExamples = true
271+
});
262272
IEdmEntityType entity = model.SchemaElements.OfType<IEdmEntityType>().First(t => t.Name == "Human");
263273
Assert.NotNull(entity); // Guard
264274

0 commit comments

Comments
 (0)