Skip to content
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

- fixes #622 a bug where root URL would not be parsed from the document #626

Merged
merged 2 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/GenerationConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> PropertiesPrefixToStrip { get; set; } = new() { "@odata."};
public HashSet<string> IgnoredRequestContentTypes { get; set; } = new();
public bool UsesBackingStore { get; set; }
Expand Down
7 changes: 7 additions & 0 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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}");
Expand Down
3 changes: 3 additions & 0 deletions tests/Kiota.Builder.IntegrationTests/ToDoApi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions tests/Kiota.Builder.Tests/KiotaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ILogger<KiotaBuilder>>();
var builder = new KiotaBuilder(mockLogger.Object, new GenerationConfiguration() { ClientClassName = "Graph", OpenAPIFilePath = tempFilePath });
await Assert.ThrowsAsync<InvalidOperationException>(() => 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<ILogger<KiotaBuilder>>();
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()
{
Expand Down