microsoft / kiota-java

Java libraries for Kiota-generated API clients.
https://aka.ms/kiota/docs
MIT License
22 stars 24 forks source link

Attribute name collision for getter method #1430

Closed Javatar81 closed 1 month ago

Javatar81 commented 1 month ago

I have an OpenAPI spec (from Keycloak) that defines a deprecated attribute named authTime (of type Integer) and another attribute called auth_time (of type Long).

Kiota generates both fields but only one getter as follows:

private Long auth_time;
@Deprecated
private Integer authTime;

public Long getAuthTime() {
    return this.auth_time;
}

This leads to the following compile error in the serialize method:

public void serialize(@jakarta.annotation.Nonnull final SerializationWriter writer) {
        ...
        // The method writeIntegerValue(String, Integer) in the type SerializationWriter is not applicable for the arguments (String, Long)
        writer.writeIntegerValue("authTime", this.getAuthTime());
        writer.writeLongValue("auth_time", this.getAuthTime());
        ...
}
baywet commented 1 month ago

Hi @Javatar81 Thanks for using kiota and for reaching out. This is most likely because the refiner which adds the getters/setters doesn't check for conflicts before it does so.

https://github.com/microsoft/kiota/blob/be75280c924381c03ddf2c005a4ba24e8e5935c8/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs#L182

We'd need to come up with a deterministic logic which does not depend on the order of the properties or does not impact the name normalization to language conventions. Maybe the logic would be to "undo" the normalization when we run into a collision (only possible when characters have been removed due to normalization). What do you think?

Javatar81 commented 1 month ago

Another option would be normalizing the field name instead of the getter name and then derive the getter name from the normalized field name. The advantage would be that the getter name would always be equal to "get" + first upper fieldName and the collision would consistently occur for fields and getters (as long as they are processed in the same order)

baywet commented 1 month ago

It should already be the case. https://github.com/microsoft/kiota/blob/be75280c924381c03ddf2c005a4ba24e8e5935c8/src/Kiota.Builder/Refiners/JavaRefiner.cs#L74 https://github.com/microsoft/kiota/blob/be75280c924381c03ddf2c005a4ba24e8e5935c8/src/Kiota.Builder/Refiners/JavaRefiner.cs#L82

microsoft-github-policy-service[bot] commented 1 month ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.