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

azure-http-specs, validate orphan model serializable #2318

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/azure-http-specs"
---

Add orphanModelSerializable operation to verify the JSON serialization of an orphan model
6 changes: 4 additions & 2 deletions packages/azure-http-specs/spec-summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,11 @@ Expected response body:
- `post /azure/client-generator-core/usage/inputToInputOutput`
- `post /azure/client-generator-core/usage/outputToInputOutput`
- `post /azure/client-generator-core/usage/modelInReadOnlyProperty`
- `post /azure/client-generator-core/usage/orphanModelSerializable`

This scenario contains two public operations. Both should be generated and exported.
The models are override to roundtrip, so they should be generated and exported as well.
This scenario contains 4 public operations. All should be generated and exported.
'OrphanModel' is not used but specified as 'public' and 'input', so it should be generated in SDK. The 'orphanModelSerializable' operation verifies that the model can be serialized to JSON.
The other models are override to roundtrip, so they should be generated and exported as well.

### Azure_Core_Basic_createOrReplace

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ namespace _Specs_.Azure.ClientGenerator.Core.Usage;

@scenario
@scenarioDoc("""
This scenario contains two public operations. Both should be generated and exported.
The models are override to roundtrip, so they should be generated and exported as well.
This scenario contains 4 public operations. All should be generated and exported.
'OrphanModel' is not used but specified as 'public' and 'input', so it should be generated in SDK. The 'orphanModelSerializable' operation verifies that the model can be serialized to JSON.
The other models are override to roundtrip, so they should be generated and exported as well.
""")
@global.Azure.ClientGenerator.Core.clientNamespace("azure.clientgenerator.core.usage", "java")
namespace ModelInOperation {
Expand Down Expand Up @@ -90,11 +91,31 @@ namespace ModelInOperation {
op modelInReadOnlyProperty(@body body: RoundTripModel): {
@body body: RoundTripModel;
};

@doc("""
Serialize the 'OrphanModel' as request body.

Expected body parameter:
```json
{
"name": "name",
"desc": "desc"
}
```
""")
@global.Azure.ClientGenerator.Core.convenientAPI(false)
@route("/orphanModelSerializable")
@put
op orphanModelSerializable(@body body: unknown): NoContentResponse;
Copy link
Member Author

@weidongxu-microsoft weidongxu-microsoft Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use the unknown to avoid ref the OrphanModel.

Let me know any language that having difficulty to serialize a OrphanModel model into the request body of this op.

}

@doc("Not used anywhere, but access is override to public so still need to be generated and exported with serialization.")
@global.Azure.ClientGenerator.Core.usage(global.Azure.ClientGenerator.Core.Usage.input)
@global.Azure.ClientGenerator.Core.access(global.Azure.ClientGenerator.Core.Access.public)
model OrphanModel {
@global.Azure.ClientGenerator.Core.clientName("modelName")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we do need to have a way to indicate how to serialize an orphan model. it seems weird that i have an orphan model that one property has a client name.

Copy link
Member Author

@weidongxu-microsoft weidongxu-microsoft Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clientName appears expected for me. This case maybe a bit uncommon that here rename is non-scoped, but I assume it be pretty common in scoped case that one language want one name, while other language want another.

And the eventgrid lib does exact this with @clientName on different SDKs.
https://github.com/Azure/azure-rest-api-specs/blob/main/specification/eventgrid/Azure.Messaging.EventGrid.SystemEvents/propertyNameOverrideCsharp.tsp
https://github.com/Azure/azure-rest-api-specs/blob/main/specification/eventgrid/Azure.Messaging.EventGrid.SystemEvents/propertyNameOverrideJava.tsp

But yes, we may need some way to say default to JSON, either argument to TCGC, option to emitter, or param to @service

name: string;

@encodedName("application/json", "desc")
description: string;
}
Comment on lines 115 to 121
Copy link
Member Author

@weidongxu-microsoft weidongxu-microsoft Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the model a little, to mix typespec name, client name, encoded name. And we expect SDK find the correct name for JSON serialization (it should be encoded name of application/json, if not available then typespec name).

Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,18 @@ Scenarios.Azure_ClientGenerator_Core_Usage_ModelInOperation = passOnSuccess([
},
kind: "MockApiDefinition",
},
{
uri: "/azure/client-generator-core/usage/orphanModelSerializable",
method: "put",
request: {
body: {
name: "name",
desc: "desc",
},
},
response: {
status: 204,
},
kind: "MockApiDefinition",
},
]);
Loading