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

Respecting JSONEncoder's keyEncodingStrategy #5

Open
jcbriones opened this issue Feb 11, 2023 · 2 comments
Open

Respecting JSONEncoder's keyEncodingStrategy #5

jcbriones opened this issue Feb 11, 2023 · 2 comments

Comments

@jcbriones
Copy link

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.

@mattpolzin
Copy link
Owner

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!

@jcbriones
Copy link
Author

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

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

No branches or pull requests

2 participants