christianhelle / refitter

A tool for generating Refit interfaces and contracts from OpenAPI specifications
https://refitter.github.io
MIT License
191 stars 41 forks source link

Support custom format-mappings via Key-Value configuration #438

Open kirides opened 2 months ago

kirides commented 2 months ago

Is your feature request related to a problem? Please describe. Sending proprietary data types which have custom serialization attached to them is bothersome as we have to manually override the generated api contracts.

{
  "type": "string",
  "format": "date-time"
}

This will be mapped to DateTimeOffset for example.

Currently it's easy to override the "format" on the server side. But it's impossible to override the client generation to make use of the newly acquired information.

Describe the solution you'd like

{
  "type": "string",
  "format": "my-date-time"
}

Having a way to override string:my-date-time (imaginary syntax) generated output. kind of like "dateType": "System.DateOnly" but for anything we need.

{
  "codeGeneratorSettings": {
    "typeOverrides": {
      "string:my-date-time": "Domain.Specific.DataType"
    }
  }
}

in Theory this should be doable by overriding how CSharpClientGeneratorSettings.CodeGeneratorSettings.TypeNameGenerator generates the type name

Describe alternatives you've considered overriding the format to make the api return an object with a property of type "object", which then allows use to use "anyObjectType": "System.Text.Json.JsonElement" to manually deserialize the type.

But this requires us to copy the used JsonSerializationOptions everywhere.

Additional context ...

christianhelle commented 2 months ago

I'm currently on holiday. I'll take a look at this in 2 weeks @kirides