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

Handle paths that contain colons #227

Merged
merged 5 commits into from
Nov 22, 2023
Merged

Handle paths that contain colons #227

merged 5 commits into from
Nov 22, 2023

Conversation

christianhelle
Copy link
Owner

This resolves #225 reported by @safakkesikci regarding OpenAPI specifications where paths include colons resulting in the generated code not being able to build

OpenApi spec:

customer.json

Refitter configuration file

{
    "openApiPath": "./customer.json",
    "namespace": "Customer",
    "naming": {
        "useOpenApiTitle": false
    },
    "multipleInterfaces": "ByTag",
    "operationNameTemplate": "{operationName}Async",
    "outputFolder": "Generated",
    "outputFilename": "Customer.cs",
    "codeGeneratorSettings": {
        "requiredPropertiesMustBeDefined": true,
        "generateDataAnnotations": true,
        "anyType": "object",
        "dateType": "System.DateTimeOffset",
        "dateTimeType": "System.DateTimeOffset",
        "timeType": "System.TimeSpan",
        "timeSpanType": "System.TimeSpan",
        "arrayType": "System.Collections.Generic.ICollection",
        "dictionaryType": "System.Collections.Generic.IDictionary",
        "arrayInstanceType": "System.Collections.ObjectModel.Collection",
        "dictionaryInstanceType": "System.Collections.Generic.Dictionary",
        "arrayBaseType": "System.Collections.ObjectModel.Collection",
        "dictionaryBaseType": "System.Collections.Generic.Dictionary",
        "propertySetterAccessModifier": "",
        "generateImmutableArrayProperties": false,
        "generateImmutableDictionaryProperties": false,
        "handleReferences": false,
        "jsonSerializerSettingsTransformationMethod": null,
        "generateJsonMethods": false,
        "enforceFlagEnums": false,
        "inlineNamedDictionaries": false,
        "inlineNamedTuples": true,
        "inlineNamedArrays": false,
        "generateOptionalPropertiesAsNullable": false,
        "generateNullableReferenceTypes": false,
        "generateNativeRecords": false,
        "generateDefaultValues": true,
        "inlineNamedAny": false,
        "excludedTypeNames": []
    }
}

Output

[Headers("Accept: application/json")]
[Get("/customers/passportinfo")]
Task<ICollection<CustomerResponse>> PassportinfoAsync(
    [Query, AliasAs("PassportNumber")] string passportNumber, 
    [Query, AliasAs("FirstName")] string firstName, 
    [Query, AliasAs("LastName")] string lastName, 
    [Query, AliasAs("CountryId")] int? countryId);

Added a new method ConvertColonsToPascalCase in the StringCasingExtensions class. This method will allow us to convert string with colons to PascalCase. The change will provide a consistent way to represent colon-separated values in PascalCase, matching our coding style convention.

Also, a new unit test method was added for it in the StringCasingExtensionTests class to ensure the functionality works as expected.
The function 'CapitalizeFirstCharacter' in the 'StringCasingExtensions' was throwing an error if a null or an empty string was passed as an argument. Added a null or empty string check to prevent the error. Also, corresponding unit tests were added to confirm that it correctly handles null or empty strings.
OperationNameGenerator is updated to include the conversion of colons to PascalCase. This change addresses the problem of having inconsistent naming convention when colon exists in string. Now it ensures a unified naming convention throughout the code which result in better readability and maintenance.
OpenAPI documents are now generated with a separate method, named `GetOpenApiDocument`. This change was introduced to address a problem with special characters appearing in our API paths. A new method, `SanitizePath` has been added to remove these special characters from paths prior to the object creation.
This commit introduces a new file, ColonsInPathTests.cs in the Refitter.Tests project. The file includes tests to ensure correct code generation, absence of colons in the generated path, and successful build of the generated code. This is done to improve our coverage of edge cases where colons might appear in API paths.
@christianhelle christianhelle added the enhancement New feature, bug fix, or request label Nov 21, 2023
@christianhelle christianhelle self-assigned this Nov 21, 2023
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Copy link

codecov bot commented Nov 21, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (4ac85ff) 98.30% compared to head (4fafe4f) 98.19%.
Report is 21 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #227      +/-   ##
==========================================
- Coverage   98.30%   98.19%   -0.12%     
==========================================
  Files          55       56       +1     
  Lines        2006     1939      -67     
==========================================
- Hits         1972     1904      -68     
- Misses         17       18       +1     
  Partials       17       17              
Flag Coverage Δ
unittests 98.19% <100.00%> (-0.12%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@christianhelle christianhelle merged commit 15639b4 into main Nov 22, 2023
@christianhelle christianhelle deleted the sanitize-path branch May 22, 2024 06:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature, bug fix, or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

if a path contain colon (":") character then must be replace it
1 participant