OpenAPIReflection icon indicating copy to clipboard operation
OpenAPIReflection copied to clipboard

Respecting JSONEncoder's keyEncodingStrategy

Open jcbriones opened this issue 2 years ago • 2 comments

Issue: Found an issue where when I use theOpenAPIEncodedSchemaType.openAPISchema(using encoder: JSONEncoder) to create a JSONSchema with passing an encoder that has keyEncodingStrategy set to convertToSnakeCase, the JSONSchema's keys wasn't converting the keys to use convertToSnakeCase and returning as what the variable name is set to.

How to reproduce:

  1. Create a JSONEncoder with the following:
let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
  1. Create a sample model
struct SampleObject: Codable, Sampleable, OpenAPIEncodedSchemaType {
    let sampleKey: String
    let sampleValue: String
}
  1. Initialize the model with any values and use the SampleObject's openAPISchema(_:) to generate the schema.
  2. Verify the keys of the model as sampleKey and sampleValue instead of sample_key and sample_value.

Expected: The expected result should be having the keys as sample_key and sample_value.

jcbriones avatar Feb 11 '23 22:02 jcbriones

This is interesting. Fundamentally the JSONEncoder passed in is used as sparingly as possible during reflection, but I can see how you would want (and reasonably expect) the encoder's settings to be respected RE things like key encoding strategy.

This seems like a worthwhile thing to fix, though I am not sure when I will get the time to tackle it.

Thanks for reporting!

mattpolzin avatar Mar 03 '23 01:03 mattpolzin

Yeah of course! Wanting to help improve this! It really is useful

jcbriones avatar Mar 03 '23 03:03 jcbriones