Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion packages/typespec-ts/src/modular/emitSamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ function prepareExampleParameters(
topLevelClient,
dpgContext,
{
onClientOnly: true
onClientOnly: true,
requiredOnly: true
Copy link
Member

Choose a reason for hiding this comment

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

We should not filter out the optional client params at the begining and we should handle optional client params during constructing the statement.

}
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Optional parameters should not appear as standalone parameters in sample code

## TypeSpec

```typespec
import "@typespec/http";
import "@typespec/rest";
import "@azure-tools/typespec-azure-core";
import "@azure-tools/typespec-client-generator-core";

using TypeSpec.Http;
using TypeSpec.Rest;
using Azure.Core;

@service(#{
title: "Sample Service",
})
@server(
"{endpoint}",
"Sample endpoint",
{
@doc("The endpoint URL")
endpoint: string = "https://example.com",
}
)
namespace SampleService;

model Person {
id: string;
name: string;
}

@route("/persons")
interface Persons {
@get
list(
@query start?: string,
@query top?: int32,
): Person[];
}
```

## Example

```json
{
"title": "List persons",
"operationId": "Persons_List",
"parameters": {
"start": "00000000-0000-0000-0000-000000000000",
"top": 20
},
"responses": {
"200": {
"body": [
{
"id": "person1",
"name": "John Doe"
}
]
}
}
}
```

## Generated Sample

```ts samples
/** This file path is /samples-dev/listSample.ts */
import { SampleServiceClient } from "@azure/internal-test";

/**
* This sample demonstrates how to execute list
*
* @summary execute list
* x-ms-original-file: 2021-10-01-preview/json.json
*/
async function listPersons(): Promise<void> {
const client = new SampleServiceClient();
const result = await client.list({ start: "00000000-0000-0000-0000-000000000000", top: 20 });
console.log(result);
}

async function main(): Promise<void> {
await listPersons();
}

main().catch(console.error);
```
Loading