From b5b4e193e8d4d8aee2ad6871068f0eec71855f26 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Tue, 15 Mar 2022 12:21:43 -0400 Subject: [PATCH] - fixes a bug where referenced types with no titles would fail to generate --- src/Kiota.Builder/KiotaBuilder.cs | 1 + .../Kiota.Builder.Tests/KiotaBuilderTests.cs | 95 +++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/src/Kiota.Builder/KiotaBuilder.cs b/src/Kiota.Builder/KiotaBuilder.cs index acf595d35e..aa128e9751 100644 --- a/src/Kiota.Builder/KiotaBuilder.cs +++ b/src/Kiota.Builder/KiotaBuilder.cs @@ -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. diff --git a/tests/Kiota.Builder.Tests/KiotaBuilderTests.cs b/tests/Kiota.Builder.Tests/KiotaBuilderTests.cs index 0789d91db0..d982037651 100644 --- a/tests/Kiota.Builder.Tests/KiotaBuilderTests.cs +++ b/tests/Kiota.Builder.Tests/KiotaBuilderTests.cs @@ -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 { + { + "name", new OpenApiSchema { + Type = "string" + } + }, + { + "kind", new OpenApiSchema { + Type = "string" + } + } + }, + Discriminator = new OpenApiDiscriminator { + PropertyName = "kind", + Mapping = new Dictionary { + { + "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 { + baseObjet, + new OpenApiSchema { + Type = "object", + Properties = new Dictionary { + { + "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 { + { + "subNS.baseObject", baseObjet + }, + { + "subNS.derivedObject", derivedObjet + } + } + }, + }; + var mockLogger = new Mock>(); + 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("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")]