-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix CSharp client BaseUrl usage #5113
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
base: master
Are you sure you want to change the base?
Conversation
When `UseBaseUrl` is set, the methods need to use the `BaseUrl` property instead of the backing field, otherwise the base class, if it exists, cannot pass the URL to a derived one fixes RicoSuter#4705
|
I have the feeling that this is just a "wrong configuration" and this PR breaks some config permutations. |
|
Could you elaborate what do you mean by "wrong configuration"? |
|
can you share your config + the generated code (part of the ctor)? |
|
Will do in four days, travelling atm |
The problem is that there are quite some configs in this areas and some config combination produce wrong code... maybe it's just a small change needed to make it work. |
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
namespace ThinkWin.Aquantify.Mobile.Portable.Services.Clients {
public class BaseClient {
private readonly AuthService _authService;
protected BaseClient(AuthService authService) {
_authService = authService;
}
protected string BaseUrl {
get => _authService.GetBaseUrl();
set { }
}
protected async Task<HttpRequestMessage> CreateHttpRequestMessageAsync(CancellationToken cancellationToken) {
var request = new HttpRequestMessage();
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", (await _authService.GetAuthenticationToken()).Token);
return request;
}
}
}{
"runtime": "Default",
"defaultVariables": null,
"documentGenerator": {
"fromDocument": {
"json": "",
"url": "https://localhost:44330/swagger/v1/swagger.json",
"output": null,
"newLineBehavior": "Auto"
}
},
"codeGenerators": {
"openApiToCSharpClient": {
"clientBaseClass": "BaseClient",
"configurationClass": "AuthService",
"generateClientClasses": true,
"suppressClientClassesOutput": false,
"generateClientInterfaces": false,
"suppressClientInterfacesOutput": false,
"clientBaseInterface": null,
"injectHttpClient": true,
"disposeHttpClient": true,
"protectedMethods": [],
"generateExceptionClasses": true,
"exceptionClass": "ApiException",
"wrapDtoExceptions": true,
"useHttpClientCreationMethod": false,
"httpClientType": "System.Net.Http.HttpClient",
"useHttpRequestMessageCreationMethod": true,
"useBaseUrl": true,
"generateBaseUrlProperty": false,
"generateSyncMethods": false,
"generatePrepareRequestAndProcessResponseAsAsyncMethods": false,
"exposeJsonSerializerSettings": false,
"clientClassAccessModifier": "public",
"typeAccessModifier": "public",
"propertySetterAccessModifier": "",
"generateNativeRecords": false,
"generateContractsOutput": false,
"contractsNamespace": null,
"contractsOutputFilePath": null,
"parameterDateTimeFormat": "s",
"parameterDateFormat": "yyyy-MM-dd",
"generateUpdateJsonSerializerSettingsMethod": true,
"useRequestAndResponseSerializationSettings": false,
"serializeTypeInformation": false,
"queryNullValue": "",
"className": "{controller}Client",
"operationGenerationMode": "MultipleClientsFromOperationId",
"additionalNamespaceUsages": [],
"additionalContractNamespaceUsages": [],
"generateOptionalParameters": false,
"generateJsonMethods": false,
"enforceFlagEnums": false,
"parameterArrayType": "System.Collections.Generic.IEnumerable",
"parameterDictionaryType": "System.Collections.Generic.IDictionary",
"responseArrayType": "System.Collections.Generic.ICollection",
"responseDictionaryType": "System.Collections.Generic.IDictionary",
"wrapResponses": false,
"wrapResponseMethods": [],
"generateResponseClasses": true,
"responseClass": "SwaggerResponse",
"namespace": "ThinkWin.Aquantify.Mobile.Portable.Services.Clients",
"requiredPropertiesMustBeDefined": true,
"dateType": "System.DateTime",
"jsonConverters": null,
"anyType": "object",
"dateTimeType": "System.DateTime",
"timeType": "System.TimeSpan",
"timeSpanType": "System.TimeSpan",
"arrayType": "System.Collections.Generic.ICollection",
"arrayInstanceType": "System.Collections.ObjectModel.Collection",
"dictionaryType": "System.Collections.Generic.IDictionary",
"dictionaryInstanceType": "System.Collections.Generic.Dictionary",
"arrayBaseType": "System.Collections.ObjectModel.Collection",
"dictionaryBaseType": "System.Collections.Generic.Dictionary",
"classStyle": "Poco",
"jsonLibrary": "NewtonsoftJson",
"generateDefaultValues": true,
"generateDataAnnotations": true,
"excludedTypeNames": [],
"excludedParameterNames": [],
"handleReferences": false,
"generateImmutableArrayProperties": false,
"generateImmutableDictionaryProperties": false,
"jsonSerializerSettingsTransformationMethod": null,
"inlineNamedArrays": false,
"inlineNamedDictionaries": false,
"inlineNamedTuples": true,
"inlineNamedAny": false,
"generateDtoTypes": true,
"generateOptionalPropertiesAsNullable": false,
"generateNullableReferenceTypes": false,
"templateDirectory": "NswagCSharpTemplates/",
"serviceHost": null,
"serviceSchemes": null,
"output": "../ThinkWin.Aquantify.Mobile.Portable/Services/Clients/Clients.cs",
"newLineBehavior": "Auto"
}
}
} |
|
@RicoSuter If you use these and generate a client it will be using |
|
I also have this problem, since version 14.0.3 when following config is present, UseBaseUrl = true,
GenerateBaseUrlProperty = false
ClientBaseClass = "ClientBase", |
When
UseBaseUrlis set, the methods need to use theBaseUrlproperty instead of the backing field, otherwise the base class, if it exists, cannot pass the URL to a derived onefixes #4705