Skip to content

Commit

Permalink
Merge branch 'main' into global-cadl-not-req
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin authored Oct 20, 2022
2 parents 0f86a5e + 2e8b5fa commit 7c0fc0a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 6 deletions.
12 changes: 6 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"type": "node",
"request": "attach",
"name": "Attach to Default Port",
"port": 9229,
Expand All @@ -21,7 +21,7 @@
}
},
{
"type": "pwa-node",
"type": "node",
"request": "attach",
"name": "Tests",
"port": 9229,
Expand All @@ -42,7 +42,7 @@
},
{
"name": "Attach to Language Server",
"type": "pwa-node",
"type": "node",
"request": "attach",
"port": 4242,
"restart": {
Expand All @@ -63,7 +63,7 @@
}
},
{
"type": "pwa-node",
"type": "node",
"request": "launch",
"name": "Compile Scratch",
"program": "${workspaceFolder}/packages/compiler/dist/core/cli.js",
Expand All @@ -86,7 +86,7 @@
}
},
{
"type": "pwa-node",
"type": "node",
"request": "launch",
"name": "Compile Scratch (nostdlib)",
"program": "${workspaceFolder}/packages/compiler/dist/core/cli.js",
Expand All @@ -105,7 +105,7 @@
},
{
"name": "Regenerate .tmlanguage",
"type": "pwa-node",
"type": "node",
"program": "${workspaceFolder}/packages/cadl-vscode/scripts/generate-tmlanguage.js",
"request": "launch",
"cwd": "${workspaceFolder}/packages/cadl-vscode",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cadl-lang/openapi3",
"comment": "Fix: @extension on a model is intrinsic types are being applied",
"type": "patch"
}
],
"packageName": "@cadl-lang/openapi3"
}
2 changes: 2 additions & 0 deletions packages/openapi3/src/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,8 @@ function createOAPIEmitter(program: Program, options: ResolvedOpenAPI3EmitterOpt
}
}

attachExtensions(program, cadlType, newTarget);

return newTarget;
}

Expand Down
16 changes: 16 additions & 0 deletions packages/openapi3/test/primitive-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ describe("openapi3: primitives", () => {
maxLength: 10,
});
});

it("includes extensions passed on the model", async () => {
const res = await oapiForModel(
"Pet",
`
@extension("x-custom", "my-value")
model Pet is string;
`
);

ok(res.schemas.Pet, "expected definition named Pet");
deepStrictEqual(res.schemas.Pet, {
type: "string",
"x-custom": "my-value",
});
});
});

describe("using @doc decorator", () => {
Expand Down
47 changes: 47 additions & 0 deletions packages/website/src/docs/standard-library/rest/operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,53 @@ model Dog extends Pet {
}
```

## Content type

### Defaults

Depending on the body of the operation http library will assume different content types:

- `bytes`: `application/octet-stream`
- `string`: `text/plain`
- an `object` or anything else: `application/json`

Examples:

```cadl
op download(): bytes; // response content type is application/octet-stream
op upload(@body file: bytes): void; // request content type is application/octet-stream
op getContent(): string; // response content type is text/plain
op getPet(): {
// response content type is application/json
name: string;
};
```

### Specify content type

The content type for an operation can be specified by including a header parameter named `contentType`.

#### Request content type

```cadl
op uploadImage(@header contentType: "image/png", @body image: bytes): void;
```

#### Response content type:

```cadl
op downloadImage(): {
@header contentType: "image/png";
@body image: bytes;
};
```

#### Multiple content types

```cadl
op uploadImage(@header contentType: "image/png" | "image/jpeg", @body image: bytes): void;
```

## Built-in response shapes

Since status codes are so common for REST APIs, Cadl comes with some built-in types for common status codes so you don't need to declare status codes so frequently.
Expand Down

0 comments on commit 7c0fc0a

Please sign in to comment.