|
| 1 | +# When emitting Python from TypeSpec, how do I rename an operation path parameter? |
| 2 | + |
| 3 | +## question |
| 4 | +I have this operation defined in TypeSpec. I know I can add the `@clientName` to change the name of the input parameter in the emitted Python operation method. This TypeSpec compiles fine (see [full TypeSpec file here](https://github.com/Azure/azure-rest-api-specs-pr/blob/feature/ai-foundry/agents-v2/specification/ai/Azure.AI.Projects/agents/operations.tsp#L56)): |
| 5 | + |
| 6 | +``` |
| 7 | +interface Agents { |
| 8 | +
|
| 9 | + @post |
| 10 | + @route("/agents/{agent_name}") |
| 11 | + @tag("Agents") |
| 12 | + updateAgent is AgentOperation< |
| 13 | + { |
| 14 | + @path |
| 15 | + @clientName("name", "python") |
| 16 | + agent_name: string; |
| 17 | +
|
| 18 | + ...UpdateAgentRequest; |
| 19 | + }, |
| 20 | + AgentObject |
| 21 | + >; |
| 22 | +
|
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +However I'm trying to avoid adding @clientName decorators in TypeSpec model definitions, and instead have them all in the client.tsp file. |
| 27 | + |
| 28 | +My question: how do I do the above, but using @@clientName in client.tsp file instead? I've tried many things, but no luck. Including: |
| 29 | + |
| 30 | +``` |
| 31 | +@@clientName(Agents.updateAgent::parameters.agent_name, "name", "python"); |
| 32 | +``` |
| 33 | + |
| 34 | +which files to compile with error `Model doesn't have member agent_name`. In the above case it's not a member but a path, so perhaps that's the reason for the error, but I don't know how to fix it. |
| 35 | + |
| 36 | +## answer |
| 37 | +You could define an alias to include path parameters then rename the path parameters by the alias like : |
| 38 | +``` |
| 39 | +
|
| 40 | +alias UpdateAgentParams = { |
| 41 | + @path originalName1: string; |
| 42 | +}; |
| 43 | +
|
| 44 | +interface MyOp { |
| 45 | + @post |
| 46 | + @route("/name/{originalName1}") |
| 47 | + updateAgent1 is AgentOperation< |
| 48 | + UpdateAgentParams, |
| 49 | + void |
| 50 | + >; |
| 51 | +
|
| 52 | + @post |
| 53 | + @route("/name/{originalName2}") |
| 54 | + updateAgent2 is AgentOperation< |
| 55 | + { |
| 56 | + @path originalName2: string; |
| 57 | + }, |
| 58 | + void |
| 59 | + >; |
| 60 | +} |
| 61 | +
|
| 62 | +@@clientName(UpdateAgentParams.originalName1, "newName1"); // OK |
| 63 | +
|
| 64 | +// Can't work since Typespec can't refer to parameter defined in an anonymous model in Template |
| 65 | +// @clientName(MyOp.updateAgent2::parameters.originalName2, "newName2"); |
| 66 | +``` |
0 commit comments