Skip to content

Commit

Permalink
- fixes a bug where referenced types with no titles would fail to gen…
Browse files Browse the repository at this point in the history
…erate
  • Loading branch information
baywet committed Mar 15, 2022
1 parent 7dd639e commit b5b4e19
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ private CodeTypeBase CreateInheritedModelDeclaration(OpenApiUrlTreeNode currentN
private static string GetReferenceIdFromOriginalSchema(OpenApiSchema schema, OpenApiSchema parentSchema) {
var title = schema.Title;
if(!string.IsNullOrEmpty(schema.Reference?.Id)) return schema.Reference.Id;
else if (string.IsNullOrEmpty(title)) return string.Empty;
if(parentSchema.Reference?.Id?.EndsWith(title, StringComparison.OrdinalIgnoreCase) ?? false) return parentSchema.Reference.Id;
if(parentSchema.Items?.Reference?.Id?.EndsWith(title, StringComparison.OrdinalIgnoreCase) ?? false) return parentSchema.Items.Reference.Id;
return (parentSchema.
Expand Down
95 changes: 95 additions & 0 deletions tests/Kiota.Builder.Tests/KiotaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,101 @@ public void UnionOfInlineSchemasWorks() {
Assert.Contains("simpleObject", typeNames);
Assert.Contains("unionTypeResponseMember1", typeNames);
}
[Fact]
public void InheritedTypeWithInlineSchemaWorks() {
var baseObjet = new OpenApiSchema {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema> {
{
"name", new OpenApiSchema {
Type = "string"
}
},
{
"kind", new OpenApiSchema {
Type = "string"
}
}
},
Discriminator = new OpenApiDiscriminator {
PropertyName = "kind",
Mapping = new Dictionary<string, string> {
{
"derivedObject", "#/components/schemas/subNS.derivedObject"
}
}
},
Reference = new OpenApiReference {
Id = "subNS.baseObject",
Type = ReferenceType.Schema
},
UnresolvedReference = false
};
var derivedObjet = new OpenApiSchema {
Type = "object",
AllOf = new List<OpenApiSchema> {
baseObjet,
new OpenApiSchema {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema> {
{
"special", new OpenApiSchema {
Type = "string"
}
}
}
}
},
Reference = new OpenApiReference {
Id = "subNS.derivedObject",
Type = ReferenceType.Schema
},
UnresolvedReference = false
};
var document = new OpenApiDocument() {
Paths = new OpenApiPaths() {
["derivedType"] = new OpenApiPathItem() {
Operations = {
[OperationType.Get] = new OpenApiOperation() {
Responses = new OpenApiResponses
{
["200"] = new OpenApiResponse {
Content = {
["application/json"] = new OpenApiMediaType {
Schema = derivedObjet
}
}
},
}
}
}
}
},
Components = new OpenApiComponents() {
Schemas = new Dictionary<string, OpenApiSchema> {
{
"subNS.baseObject", baseObjet
},
{
"subNS.derivedObject", derivedObjet
}
}
},
};
var mockLogger = new Mock<ILogger<KiotaBuilder>>();
var builder = new KiotaBuilder(mockLogger.Object, new GenerationConfiguration() { ClientClassName = "Graph", ApiRootUrl = "https://localhost" });
var node = builder.CreateUriSpace(document);
var codeModel = builder.CreateSourceModel(node);
var requestBuilderNS = codeModel.FindNamespaceByName("ApiSdk.derivedType");
Assert.NotNull(requestBuilderNS);
var requestBuilderClass = requestBuilderNS.FindChildByName<CodeClass>("derivedTypeRequestBuilder", false);
Assert.NotNull(requestBuilderClass);
var requestExecutorMethod = requestBuilderClass.Methods.FirstOrDefault(x => x.IsOfKind(CodeMethodKind.RequestExecutor));
Assert.NotNull(requestExecutorMethod);
var executorReturnType = requestExecutorMethod.ReturnType as CodeType;
Assert.NotNull(executorReturnType);
Assert.Contains("DerivedObject", requestExecutorMethod.ReturnType.Name);
}
[InlineData("string", "", "string")]// https://spec.openapis.org/registry/format/
[InlineData("string", "commonmark", "string")]
[InlineData("string", "html", "string")]
Expand Down

0 comments on commit b5b4e19

Please sign in to comment.