diff --git a/CHANGELOG.md b/CHANGELOG.md index b53413d2f2..d0642bd64c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixes a bug where float, long and binary types would not be parsed by the generator #558 - Fixes a bug where generation would fail on compact namespace names #558 - Renames request info into request information to avoid conflicts with platform #559 +- Fixes a bug where the server url would not be taken in consideration #626 ## [0.0.8] - 2021-08-25 diff --git a/src/Kiota.Builder/GenerationConfiguration.cs b/src/Kiota.Builder/GenerationConfiguration.cs index 0fc2441b94..e2ee08d409 100644 --- a/src/Kiota.Builder/GenerationConfiguration.cs +++ b/src/Kiota.Builder/GenerationConfiguration.cs @@ -7,7 +7,7 @@ public class GenerationConfiguration { public string ClientClassName { get; set; } = "ApiClient"; public string ClientNamespaceName { get; set; } = "ApiSdk"; public GenerationLanguage Language { get; set; } = GenerationLanguage.CSharp; - public string ApiRootUrl { get; set; } = "https://graph.microsoft.com/v1.0"; + public string ApiRootUrl { get; set; } public List PropertiesPrefixToStrip { get; set; } = new() { "@odata."}; public HashSet IgnoredRequestContentTypes { get; set; } = new(); public bool UsesBackingStore { get; set; } diff --git a/src/Kiota.Builder/KiotaBuilder.cs b/src/Kiota.Builder/KiotaBuilder.cs index cc1d8e68e3..97f167625b 100644 --- a/src/Kiota.Builder/KiotaBuilder.cs +++ b/src/Kiota.Builder/KiotaBuilder.cs @@ -53,6 +53,8 @@ public async Task GenerateSDK() var doc = CreateOpenApiDocument(input); StopLogAndReset(sw, "step 2 - parsing the document - took"); + SetApiRootUrl(doc); + // Step 3 - Create Uri Space of API sw.Start(); var openApiTree = CreateUriSpace(doc); @@ -73,6 +75,11 @@ public async Task GenerateSDK() await CreateLanguageSourceFilesAsync(config.Language, generatedCode); StopLogAndReset(sw, "step 6 - writing files - took"); } + private void SetApiRootUrl(OpenApiDocument doc) { + config.ApiRootUrl = doc.Servers.FirstOrDefault()?.Url.TrimEnd('/'); + if(string.IsNullOrEmpty(config.ApiRootUrl)) + throw new InvalidOperationException("A servers entry (v3) or host + basePath + schems properties (v2) must be present in the OpenAPI description."); + } private void StopLogAndReset(Stopwatch sw, string prefix) { sw.Stop(); logger.LogDebug($"{prefix} {sw.Elapsed}"); diff --git a/tests/Kiota.Builder.IntegrationTests/ToDoApi.yaml b/tests/Kiota.Builder.IntegrationTests/ToDoApi.yaml index 19ac93c13e..e3c8ab4c65 100644 --- a/tests/Kiota.Builder.IntegrationTests/ToDoApi.yaml +++ b/tests/Kiota.Builder.IntegrationTests/ToDoApi.yaml @@ -2,6 +2,9 @@ openapi: 3.0.0 info: title: "Todo API" version: "1.0.0" +servers: + - url: https://mytodos.doesnotexist/ + description: Core paths: /todos: get: diff --git a/tests/Kiota.Builder.Tests/KiotaBuilderTests.cs b/tests/Kiota.Builder.Tests/KiotaBuilderTests.cs index 50ceeb36df..6be65289e4 100644 --- a/tests/Kiota.Builder.Tests/KiotaBuilderTests.cs +++ b/tests/Kiota.Builder.Tests/KiotaBuilderTests.cs @@ -6,11 +6,32 @@ using Xunit; using System.Collections.Generic; using Microsoft.OpenApi.Any; +using System.Threading.Tasks; +using System.IO; +using System; namespace Kiota.Builder.Tests { public class KiotaBuilderTests { + [Fact] + public async Task ThrowsOnMissingServer() { + var tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); + await File.WriteAllLinesAsync(tempFilePath, new string[] {"openapi: 3.0.0", "info:", " title: \"Todo API\"", " version: \"1.0.0\""}); + var mockLogger = new Mock>(); + var builder = new KiotaBuilder(mockLogger.Object, new GenerationConfiguration() { ClientClassName = "Graph", OpenAPIFilePath = tempFilePath }); + await Assert.ThrowsAsync(() => builder.GenerateSDK()); + File.Delete(tempFilePath); + } + [Fact] + public async Task DoesntThrowOnMissingServerForV2() { + var tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); + await File.WriteAllLinesAsync(tempFilePath, new string[] {"swagger: 2.0", "title: \"Todo API\"", "version: \"1.0.0\"", "host: mytodos.doesntexit", "basePath: v2", "schemes:", " - https"," - http"}); + var mockLogger = new Mock>(); + var builder = new KiotaBuilder(mockLogger.Object, new GenerationConfiguration() { ClientClassName = "Graph", OpenAPIFilePath = tempFilePath }); + await builder.GenerateSDK(); + File.Delete(tempFilePath); + } [Fact] public void Single_root_node_creates_single_request_builder_class() {