Skip to content

Commit

Permalink
- fixes #1896 replaces exception by warn for missing host in descript…
Browse files Browse the repository at this point in the history
…ions

Signed-off-by: Vincent Biret <vibiret@microsoft.com>
  • Loading branch information
baywet committed Oct 17, 2022
1 parent cb4de9a commit ea8ac22
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void FilterPathsByPatterns(OpenApiDocument doc, List<Glob> includePattern
private void SetApiRootUrl() {
config.ApiRootUrl = openApiDocument.Servers.FirstOrDefault()?.Url.TrimEnd('/');
if(string.IsNullOrEmpty(config.ApiRootUrl))
throw new InvalidOperationException("A servers entry (v3) or host + basePath + schemes properties (v2) must be present in the OpenAPI description.");
logger.LogWarning("A servers entry (v3) or host + basePath + schemes properties (v2) was not present in the OpenAPI description. The root URL will need to be set manually with the request adapter.");
}
private void StopLogAndReset(Stopwatch sw, string prefix) {
sw.Stop();
Expand Down
10 changes: 6 additions & 4 deletions src/Kiota.Builder/Writers/CSharp/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,12 @@ private static void WriteApiConstructorBody(CodeClass parentClass, CodeMethod me
var requestAdapterPropertyName = requestAdapterProperty.Name.ToFirstCharacterUpperCase();
WriteSerializationRegistration(method.SerializerModules, writer, "RegisterDefaultSerializer");
WriteSerializationRegistration(method.DeserializerModules, writer, "RegisterDefaultDeserializer");
writer.WriteLine($"if (string.IsNullOrEmpty({requestAdapterPropertyName}.BaseUrl)) {{");
writer.IncreaseIndent();
writer.WriteLine($"{requestAdapterPropertyName}.BaseUrl = \"{method.BaseUrl}\";");
writer.CloseBlock();
if (!string.IsNullOrEmpty(method.BaseUrl)) {
writer.WriteLine($"if (string.IsNullOrEmpty({requestAdapterPropertyName}.BaseUrl)) {{");
writer.IncreaseIndent();
writer.WriteLine($"{requestAdapterPropertyName}.BaseUrl = \"{method.BaseUrl}\";");
writer.CloseBlock();
}
if (backingStoreParameter != null)
writer.WriteLine($"{requestAdapterPropertyName}.EnableBackingStore({backingStoreParameter.Name});");
}
Expand Down
10 changes: 6 additions & 4 deletions src/Kiota.Builder/Writers/Go/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,12 @@ private void WriteApiConstructorBody(CodeClass parentClass, CodeMethod method, L
var backingStoreParameter = method.Parameters.FirstOrDefault(x => x.IsOfKind(CodeParameterKind.BackingStore));
WriteSerializationRegistration(method.SerializerModules, writer, parentClass, "RegisterDefaultSerializer", "SerializationWriterFactory");
WriteSerializationRegistration(method.DeserializerModules, writer, parentClass, "RegisterDefaultDeserializer", "ParseNodeFactory");
writer.WriteLine($"if m.{requestAdapterPropertyName}.GetBaseUrl() == \"\" {{");
writer.IncreaseIndent();
writer.WriteLine($"m.{requestAdapterPropertyName}.SetBaseUrl(\"{method.BaseUrl}\")");
writer.CloseBlock();
if (!string.IsNullOrEmpty(method.BaseUrl)) {
writer.WriteLine($"if m.{requestAdapterPropertyName}.GetBaseUrl() == \"\" {{");
writer.IncreaseIndent();
writer.WriteLine($"m.{requestAdapterPropertyName}.SetBaseUrl(\"{method.BaseUrl}\")");
writer.CloseBlock();
}
if(backingStoreParameter != null)
writer.WriteLine($"m.{requestAdapterPropertyName}.EnableBackingStore({backingStoreParameter.Name});");
}
Expand Down
10 changes: 6 additions & 4 deletions src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,12 @@ private static void WriteApiConstructorBody(CodeClass parentClass, CodeMethod me
var requestAdapterPropertyName = requestAdapterProperty.Name.ToFirstCharacterLowerCase();
WriteSerializationRegistration(method.SerializerModules, writer, "registerDefaultSerializer");
WriteSerializationRegistration(method.DeserializerModules, writer, "registerDefaultDeserializer");
writer.WriteLine($"if ({requestAdapterPropertyName}.getBaseUrl() == null || {requestAdapterPropertyName}.getBaseUrl().isEmpty()) {{");
writer.IncreaseIndent();
writer.WriteLine($"{requestAdapterPropertyName}.setBaseUrl(\"{method.BaseUrl}\");");
writer.CloseBlock();
if(!string.IsNullOrEmpty(method.BaseUrl)) {
writer.WriteLine($"if ({requestAdapterPropertyName}.getBaseUrl() == null || {requestAdapterPropertyName}.getBaseUrl().isEmpty()) {{");
writer.IncreaseIndent();
writer.WriteLine($"{requestAdapterPropertyName}.setBaseUrl(\"{method.BaseUrl}\");");
writer.CloseBlock();
}
if(backingStoreParameter != null)
writer.WriteLine($"this.{requestAdapterPropertyName}.enableBackingStore({backingStoreParameter.Name});");
}
Expand Down
10 changes: 6 additions & 4 deletions src/Kiota.Builder/Writers/Php/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,10 +498,12 @@ private static void WriteApiConstructorBody(CodeClass parentClass, CodeMethod co
var requestAdapterProperty = parentClass.GetPropertyOfKind(CodePropertyKind.RequestAdapter);
WriteSerializationRegistration(codeMethod.SerializerModules, writer, "registerDefaultSerializer");
WriteSerializationRegistration(codeMethod.DeserializerModules, writer, "registerDefaultDeserializer");
writer.WriteLines($"if (empty({GetPropertyCall(requestAdapterProperty, string.Empty)}->getBaseUrl())) {{");
writer.IncreaseIndent();
writer.WriteLine($"{GetPropertyCall(requestAdapterProperty, string.Empty)}->setBaseUrl('{codeMethod.BaseUrl}');");
writer.CloseBlock();
if(!string.IsNullOrEmpty(codeMethod.BaseUrl)) {
writer.WriteLines($"if (empty({GetPropertyCall(requestAdapterProperty, string.Empty)}->getBaseUrl())) {{");
writer.IncreaseIndent();
writer.WriteLine($"{GetPropertyCall(requestAdapterProperty, string.Empty)}->setBaseUrl('{codeMethod.BaseUrl}');");
writer.CloseBlock();
}
}

private static void WriteSerializationRegistration(HashSet<string> serializationModules, LanguageWriter writer, string methodName) {
Expand Down
10 changes: 6 additions & 4 deletions src/Kiota.Builder/Writers/Python/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ private static void WriteApiConstructorBody(CodeClass parentClass, CodeMethod me
var requestAdapterPropertyName = requestAdapterProperty.Name.ToSnakeCase();
WriteSerializationRegistration(method.SerializerModules, writer, "register_default_serializer");
WriteSerializationRegistration(method.DeserializerModules, writer, "register_default_deserializer");
writer.WriteLine($"if not {requestAdapterPropertyName}.base_url:");
writer.IncreaseIndent();
writer.WriteLine($"{requestAdapterPropertyName}.base_url = \"{method.BaseUrl}\"");
writer.DecreaseIndent();
if(!string.IsNullOrEmpty(method.BaseUrl)) {
writer.WriteLine($"if not {requestAdapterPropertyName}.base_url:");
writer.IncreaseIndent();
writer.WriteLine($"{requestAdapterPropertyName}.base_url = \"{method.BaseUrl}\"");
writer.DecreaseIndent();
}
if(backingStoreParameter != null)
writer.WriteLine($"self.{requestAdapterPropertyName}.enable_backing_store({backingStoreParameter.Name})");
}
Expand Down
3 changes: 2 additions & 1 deletion src/Kiota.Builder/Writers/Ruby/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ private static void WriteApiConstructorBody(CodeClass parentClass, CodeMethod me
var requestAdapterPropertyName = requestAdapterProperty.Name.ToSnakeCase();
if (method.Parameters.FirstOrDefault(x => x.IsOfKind(CodeParameterKind.RequestAdapter)) is CodeParameter requestAdapterParameter)
writer.WriteLine($"@{requestAdapterPropertyName} = {requestAdapterParameter.Name.ToSnakeCase()}");
writer.WriteLine($"{requestAdapterPropertyName}.set_base_url('{method.BaseUrl}')");
if(!string.IsNullOrEmpty(method.BaseUrl))
writer.WriteLine($"{requestAdapterPropertyName}.set_base_url('{method.BaseUrl}')");
}
private static void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMethod, LanguageWriter writer, bool inherits) {
if(inherits)
Expand Down
10 changes: 6 additions & 4 deletions src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,12 @@ private static void WriteApiConstructorBody(CodeClass parentClass, CodeMethod me
var requestAdapterPropertyName = requestAdapterProperty.Name.ToFirstCharacterLowerCase();
WriteSerializationRegistration(method.SerializerModules, writer, "registerDefaultSerializer");
WriteSerializationRegistration(method.DeserializerModules, writer, "registerDefaultDeserializer");
writer.WriteLine($"if ({requestAdapterPropertyName}.baseUrl === undefined || {requestAdapterPropertyName}.baseUrl === \"\") {{");
writer.IncreaseIndent();
writer.WriteLine($"{requestAdapterPropertyName}.baseUrl = \"{method.BaseUrl}\";");
writer.CloseBlock();
if(!string.IsNullOrEmpty(method.BaseUrl)) {
writer.WriteLine($"if ({requestAdapterPropertyName}.baseUrl === undefined || {requestAdapterPropertyName}.baseUrl === \"\") {{");
writer.IncreaseIndent();
writer.WriteLine($"{requestAdapterPropertyName}.baseUrl = \"{method.BaseUrl}\";");
writer.CloseBlock();
}
if(backingStoreParameter != null)
writer.WriteLine($"this.{requestAdapterPropertyName}.enableBackingStore({backingStoreParameter.Name});");
}
Expand Down
9 changes: 0 additions & 9 deletions tests/Kiota.Builder.Tests/KiotaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@
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[] {"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.GenerateClientAsync(new()));
File.Delete(tempFilePath);
}
[Fact]
public async Task ParsesEnumDescriptions() {
var tempFilePath = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
Expand Down

0 comments on commit ea8ac22

Please sign in to comment.