-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
[REQ] Kotlin, request for feedback on proposed model pattern for 3.1.0 spec processing #14657
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
Comments
I am not sure I understand your proposal completely. Having enum with a value property is definitely one solution. Could even be value class? What about using backticks? Would that work? |
The keys of required and optional properties could be value classes but only one value would ever be used. So I think that enums make more sense for known keys. For additional properties the key could be a value class. What is a code sample of how back ticks would work? |
As I test this, the backticks really only allow you to put numbers at the start of a variable name and escape the keywords. They do allow more but only in test method names. So they are not useful here. |
@spacether, backticks need to be used for properties that would match Kotlin's reserved keywords. For a full reference, see Kotlin docs: Keywords and operators. Is that the answer to your question?
|
Do people prefer the above solution or explicit getters instead? |
The primary need for backticks arose from compatibility with Java libraries. For example, one may want to use Hamcrest's Given the choice between foo.`value` and foo.getValue() I believe the latter is more preferable since it provides a universal access towards properties. |
Having getters will not easily cover corner cases for similarly named variables like
One model can have all of those properties and the getter should not collide. Advanced getter logic would need to be written to handle that use case. |
Sure. Such design would be quite terrible. Generating getters (e.g., fun getSomeProp() // someProp
fun `getSomeProp`() // SomeProp is not allowed. It will result in colliding names. However, generating all three of them as backticked properties: val `someProp`: Any
val `SomeProp`: Any
val `some-prop`: Any seems like a pretty big sacrifice of convenience for the sake of a corner case. I would still like to see backticks only when the naming is not allowed in Kotlin (e.g., |
This corner case is allowed by json schema, though unlikely. It looks like proto gets around this by requiring that variables follow their style guide (lowercase with underscores) which prevents collision. |
It would be interesting and useful to analyze specs in the wild and see how many key names
|
Can someone point me to a regex or implemntation that can check if a string is a valid Kotlin identifier?
If someone can get me the Kotlin info, I can run the numbers for Kotlin too. |
Another option for this very rare corner case (1.4%) of all required keys is for keys that can't produce valid in-language identifiers, depend up on the developer to extract the value from a map using map.get(key)
|
I guess the closest thing is the |
How does the kotlin client generator handle properties with invalid names? One could inherit from HashMap and make a class where one could create enums for each known key. And use those to perform type safe extraction of map values.
Here is an example which works for:
For example given this openapi component schema:
Here is a sample Kotlin Class:
Example usage code:
What do people think of this solution?
This could be a way of implementing type object models which allows:
Note:
A solution like this would also work for ArrayList (json schema type array) in openapi 3.1.0. In that version of the spec, array values depend upon the index being retrieved via prefixItems + items definitions.
The text was updated successfully, but these errors were encountered: