-
Notifications
You must be signed in to change notification settings - Fork 215
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
tsp, dpg, keep enum type for client parameters, not change it to string type #4911
base: main
Are you sure you want to change the base?
tsp, dpg, keep enum type for client parameters, not change it to string type #4911
Conversation
No changes needing a change description found. |
You can try these changes here
|
2d26fa8
to
ee68228
Compare
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ServiceClient; | ||
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.ServiceClientProperty; | ||
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.TestContext; | ||
import com.microsoft.typespec.http.client.generator.core.model.clientmodel.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, do no import *
returnType = ClassType.STRING; | ||
boolean isPathOrURIParameter = RequestParameterLocation.PATH == parameterRequestLocation | ||
|| RequestParameterLocation.URI == parameterRequestLocation; | ||
boolean isEnumAsString = !(JavaSettings.getInstance().isDataPlaneClient() && isPathOrURIParameter); | ||
if (isEnumAsString) { // do not change enum to string type for data-plane path/url parameter | ||
returnType = ClassType.STRING; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering why isPathOrURIParameter
matters here. Can we just say "DPG (from swagger) is string; anything from TypeSpec is enum" (the difference should be based on swagger / typespec)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to limit the scope to path/uri parameter only to reduce the impact, but using swagger/typespec to decide whether to make enum as string is simpler and more straightforward. I will try to use swagger/typespec to decide whether to make enum as string, and see the impact.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think just try in ServiceClientMapper
, call this method if swagger, don't call it if typespec (there should be a subclass for typespec).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If adding the condition to ServiceClientMapper
, we will also need to add it to ProxyParameterMapper
, otherwise generated proxy method will have issue, it will use string type instead of enum type. But if it is typespec, in ProxyParameterMapper
, we also need to execute other logics except the enum part logic in removeModelFromParameter
, e.g. below part will change body type to BinaryData
Lines 224 to 225 in 27d1069
if (parameterRequestLocation == RequestParameterLocation.BODY) { | |
returnType = ClassType.BINARY_DATA; |
Adding the checking logics to ProxyParameterMapper
might make the checking a bit complex. That's why I added the logic in SchemaUtil
currently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, proxy method parameter should indeed be String. This is consistent with DPG protocol method / proxy method (that parameter be String and BinaryData).
The conversion seems should be done some other places (maybe protocol method?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is real difficulty, I am fine keep the Enum on proxy method (if it is parameter from client).
Just don't condition on path/uri (I think swagger can have Enum type path/uri parameter from client too). Condition should be on swagger/typespec (probably also on parameter location -- I think we don't want to affect parameter from method).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make sense, I'm trying to add the conversion logic to change enum type to string type and keep enum type in proxy method be string.
expr = String.format("Configuration.getGlobalConfiguration().get(\"%1$s\", %2$s)", | ||
serviceClientProperty.getName().toUpperCase(Locale.ROOT), ClassType.STRING | ||
.defaultValueExpression(serviceClientProperty.getName().toLowerCase(Locale.ROOT))); | ||
if (serviceClientProperty.getType() instanceof EnumType) { | ||
expr = String.format( | ||
"%1$s.fromString(Configuration.getGlobalConfiguration().get(\"%2$s\", %3$s))", | ||
serviceClientProperty.getType(), | ||
serviceClientProperty.getName().toUpperCase(Locale.ROOT), | ||
ClassType.STRING.defaultValueExpression( | ||
serviceClientProperty.getName().toLowerCase(Locale.ROOT))); | ||
|
||
} else { | ||
expr = String.format("Configuration.getGlobalConfiguration().get(\"%1$s\", %2$s)", | ||
serviceClientProperty.getName().toUpperCase(Locale.ROOT), | ||
ClassType.STRING.defaultValueExpression( | ||
serviceClientProperty.getName().toLowerCase(Locale.ROOT))); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could there be problem of non-string enum here? @XiaofeiCao
Though I guess the probability that we have non-string enum here be pretty low. (but if it could happen, better to add a TODO for reminder)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, for EnumType's deserialization method, we should call ((EnumType) serviceClientProperty.getType()).getFromMethodName()
.
…s-enum # Conflicts: # packages/http-client-java/generator/http-client-generator-test/src/main/java/client/structure/service/ClientAClientBuilder.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/client/structure/service/ClientBClientBuilder.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/client/structure/service/FirstClientBuilder.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/client/structure/service/RenamedOperationClientBuilder.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/client/structure/service/ServiceClientClientBuilder.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/client/structure/service/TwoOperationGroupClientBuilder.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/client/structure/service/subnamespace/SecondClientBuilder.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/added/AddedClientBuilder.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/added/implementation/AddedClientImpl.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/added/implementation/InterfaceV2sImpl.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/added/models/Versions.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/madeoptional/MadeOptionalClientBuilder.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/madeoptional/implementation/MadeOptionalClientImpl.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/madeoptional/models/Versions.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/removed/RemovedClientBuilder.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/removed/implementation/RemovedClientImpl.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/removed/models/Versions.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/renamedfrom/RenamedFromClientBuilder.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/renamedfrom/implementation/NewInterfacesImpl.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/renamedfrom/implementation/RenamedFromClientImpl.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/renamedfrom/models/Versions.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/returntypechangedfrom/ReturnTypeChangedFromClientBuilder.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/returntypechangedfrom/implementation/ReturnTypeChangedFromClientImpl.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/typechangedfrom/TypeChangedFromClientBuilder.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/typechangedfrom/implementation/TypeChangedFromClientImpl.java # packages/http-client-java/generator/http-client-generator-test/src/main/java/versioning/typechangedfrom/models/Versions.java # packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/versioning-added_apiview_properties.json # packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/versioning-madeoptional_apiview_properties.json # packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/versioning-removed_apiview_properties.json # packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/versioning-renamedfrom_apiview_properties.json # packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/versioning-returntypechangedfrom_apiview_properties.json # packages/http-client-java/generator/http-client-generator-test/src/main/resources/META-INF/versioning-typechangedfrom_apiview_properties.json # packages/http-client-java/generator/http-client-generator-test/src/test/java/client/structure/ClientOperationGroupTests.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/client/structure/DefaultClientTests.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/client/structure/MultiClientTests.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/client/structure/OperationGroupClientTests.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/client/structure/RenameOperationTests.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/client/structure/service/generated/ClientAClientTestBase.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/client/structure/service/generated/FirstClientTestBase.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/client/structure/service/generated/RenamedOperationClientTestBase.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/client/structure/service/generated/ServiceClientClientTestBase.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/client/structure/service/generated/TwoOperationGroupClientTestBase.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/type/scalar/ScalarTests.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/versioning/added/AddedClientTests.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/versioning/added/generated/AddedClientTestBase.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/versioning/madeoptional/MadeOptionalClienTests.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/versioning/madeoptional/generated/MadeOptionalClientTestBase.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/versioning/removed/RemovedClientTests.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/versioning/removed/generated/RemovedClientTestBase.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/versioning/renamedfrom/RenamedFromClientTests.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/versioning/renamedfrom/generated/RenamedFromClientTestBase.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/versioning/returntypechangedfrom/generated/ReturnTypeChangedFromClientTestBase.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/versioning/typechangedfrom/TypeChangedFromClientTests.java # packages/http-client-java/generator/http-client-generator-test/src/test/java/versioning/typechangedfrom/generated/TypeChangedFromClientTestBase.java
Fix Azure/autorest.java#2974
related with https://gist.github.com/haolingdong-msft/df3f25a048baf83d8fabf5206aacc7db#8-clienttype-will-be-generated-after-adopting-tcgc
Existing code
We change enum type to string type in two classes:
typespec/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ProxyParameterMapper.java
Lines 69 to 71 in 08384aa
typespec/packages/http-client-java/generator/http-client-generator-core/src/main/java/com/microsoft/typespec/http/client/generator/core/mapper/ServiceClientMapper.java
Lines 235 to 239 in 08384aa
Logic in this pr
Above classes calls
SchemaUtil.removeModelFromParameter
to remove enum type and change it to string type.Currently I modify the logic in
removeModelFromParameter()
to not chang enum type to string type for dataplane path/uri parameters.Would like to hear your thoughts on below: @weidongxu-microsoft @XiaofeiCao
SchemaUtil
class.path-parameter-enum-as-string
.Latest logics
After discussing with Weidong, I decided to put the logics into the mappers.