-
Notifications
You must be signed in to change notification settings - Fork 253
Redundant type: object
emitted in 2.x preview
#2342
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
Comments
Hi @bkoelman, thank you for raising this. Let me investigate this and get back to you. |
@bkoelman Unfortunately the specification says this about the nullable keyword:
https://spec.openapis.org/oas/v3.0.4.html#fixed-fields-20 We fixed this issue with non-compliance with the specification. However, apparently now we need to go fix Kiota. I have opened an issue microsoft/kiota#6510 |
@darrelmiller Thanks for looking into this. In that case, I suppose all consumers will need to adapt then. |
I conducted further research on this topic and found the motivation at https://github.com/OAI/OpenAPI-Specification/blob/main/proposals/2019-10-31-Clarify-Nullable.md for why Based on my experiments,
For example, the following program (built against preview.17): using System.Text;
using Microsoft.OpenApi;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.Interfaces;
using Microsoft.OpenApi.Models.References;
using Microsoft.OpenApi.Writers;
var document = CreateChoiceDocument();
var builder = new StringBuilder();
using (var writer = new StringWriter(builder))
{
document.SerializeAs(OpenApiSpecVersion.OpenApi3_0, new OpenApiYamlWriter(writer));
}
var json = builder.ToString();
Console.WriteLine(json);
static OpenApiDocument CreateChoiceDocument()
{
var integerSchema = new OpenApiSchema
{
Type = JsonSchemaType.Integer | JsonSchemaType.Null
};
var booleanSchema = new OpenApiSchema
{
Type = JsonSchemaType.Boolean
};
var refIntegerSchema = new OpenApiSchemaReference("integerSchema");
var refBooleanSchema = new OpenApiSchemaReference("booleanSchema");
var choiceSchema = new OpenApiSchema
{
OneOf =
[
refIntegerSchema, refBooleanSchema
],
Type = JsonSchemaType.Null
};
return new OpenApiDocument
{
Components = new OpenApiComponents
{
Schemas = new Dictionary<string, IOpenApiSchema>
{
["integerSchema"] = integerSchema,
["booleanSchema"] = booleanSchema,
["choiceSchema"] = choiceSchema
}
}
};
} Prints the following: openapi: 3.0.4
info: { }
paths: { }
components:
schemas:
integerSchema:
type: integer
nullable: true
booleanSchema:
type: boolean
choiceSchema:
nullable: true
type: object
oneOf:
- $ref: '#/components/schemas/integerSchema'
- $ref: '#/components/schemas/booleanSchema' This is incorrect, because the type is not an object. This case reflects interpretation 1 mentioned above. But leaving out For example, the program: using System.Text;
using Microsoft.OpenApi;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Models.Interfaces;
using Microsoft.OpenApi.Models.References;
using Microsoft.OpenApi.Writers;
var document = CreateEntityDocument();
var builder = new StringBuilder();
using (var writer = new StringWriter(builder))
{
document.SerializeAs(OpenApiSpecVersion.OpenApi3_0, new OpenApiYamlWriter(writer));
}
var json = builder.ToString();
Console.WriteLine(json);
static OpenApiDocument CreateEntityDocument()
{
var idSchema = new OpenApiSchema
{
Properties = new Dictionary<string, IOpenApiSchema>
{
["Id"] = new OpenApiSchema
{
Type = JsonSchemaType.Integer
}
}
};
var refIdSchema = new OpenApiSchemaReference("EntityOfInt");
var requestBodySchema = new OpenApiSchema
{
Properties = new Dictionary<string, IOpenApiSchema>
{
["Person"] = new OpenApiSchema
{
Type = JsonSchemaType.Null, // must add JsonSchemaType.Object to make it appear in the output
AllOf =
[
refIdSchema, new OpenApiSchema
{
Properties = new Dictionary<string, IOpenApiSchema>
{
["Name"] = new OpenApiSchema
{
Type = JsonSchemaType.String
}
}
}
]
}
}
};
return new OpenApiDocument
{
Components = new OpenApiComponents
{
Schemas = new Dictionary<string, IOpenApiSchema>
{
["EntityOfInt"] = idSchema,
["RequestBody"] = requestBodySchema
}
}
};
} Prints the following output: openapi: 3.0.4
info: { }
paths: { }
components:
schemas:
EntityOfInt:
properties:
Id:
type: integer
RequestBody:
properties:
Person:
nullable: true # should not appear, according to the latest 3.0.x spec
type: object # should not appear, according to the latest 3.0.x spec
allOf:
- $ref: '#/components/schemas/EntityOfInt'
- properties:
Name:
type: string The problem is this. How client code generators behave today is:
Options:
All in all, changing the current behavior in Microsoft.OpenApi to comply with the latest patch of the spec has a considerable ripple effect. The meaning of @darrelmiller What are your thoughts? |
Describe the bug
Trying to use 2.0 preview 17, I see the following change in the output document, compared to 1.x:
It seems that whenever
OpenApiSchema.Type
is set toJsonSchemaType.Null
from code, this results in also emitting"type": "object"
. This is redundant in many cases, for example whenallOf
is used.While probably technically still correct, this difference makes it a pain to assess whether the new version produces a similar output document in our test suite. We have about 12,000 tests, many of which compare fragments of the OAS file, failing on changes like this. The fact that
"nullable": true
moved to the top doesn't help either, but I suspect that will be addressed in a future preview, tracked at #1314.So I wonder if this change was intentional. And if not, how hard it would be to not emit
"type": "object"
when the schema containsallOf
,oneOf
, etc.The text was updated successfully, but these errors were encountered: