Skip to content

Fix CSharp client BaseUrl usage#5113

Open
MaximBalaganskiy wants to merge 1 commit intoRicoSuter:masterfrom
MaximBalaganskiy:patch-1
Open

Fix CSharp client BaseUrl usage#5113
MaximBalaganskiy wants to merge 1 commit intoRicoSuter:masterfrom
MaximBalaganskiy:patch-1

Conversation

@MaximBalaganskiy
Copy link
Copy Markdown

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 #4705

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
@RicoSuter
Copy link
Copy Markdown
Owner

I have the feeling that this is just a "wrong configuration" and this PR breaks some config permutations.

@MaximBalaganskiy
Copy link
Copy Markdown
Author

Could you elaborate what do you mean by "wrong configuration"?

@RicoSuter
Copy link
Copy Markdown
Owner

can you share your config + the generated code (part of the ctor)?

@MaximBalaganskiy
Copy link
Copy Markdown
Author

Will do in four days, travelling atm

@RicoSuter
Copy link
Copy Markdown
Owner

Could you elaborate what do you mean by "wrong configuration"?

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.

@MaximBalaganskiy
Copy link
Copy Markdown
Author

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"
    }
  }
}

@MaximBalaganskiy
Copy link
Copy Markdown
Author

MaximBalaganskiy commented Aug 6, 2025

@RicoSuter If you use these and generate a client it will be using _baseUrl which I cannot override in BaseClient.
The only thing the fix does is it makes clients to access _baseUrl via a property (which would return the field in the default case anyway)

@ds1371dani
Copy link
Copy Markdown

I also have this problem, since version 14.0.3 when following config is present, _baseUrl is used instead of the BaseUrl created in the Base class:

UseBaseUrl = true,
GenerateBaseUrlProperty = false
ClientBaseClass = "ClientBase",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C# code generation with /GenerateBaseUrlProperty:false option is broken in v14.0.1

3 participants