-
-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Platform: add OpenAPI.fromApi tests + docs refactoring (#4226)
- Loading branch information
Showing
10 changed files
with
3,928 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
--- | ||
"@effect/platform": patch | ||
--- | ||
|
||
Ensure the encoding kind of success responses is respected in the OpenAPI spec for GET requests. | ||
|
||
Before | ||
|
||
When generating an OpenAPI spec for a GET request with a success schema of type `HttpApiSchema.Text()``, the response content type was incorrectly set to "application/json" instead of "text/plain". | ||
|
||
```ts | ||
import { | ||
HttpApi, | ||
HttpApiEndpoint, | ||
HttpApiGroup, | ||
HttpApiSchema, | ||
OpenApi | ||
} from "@effect/platform" | ||
|
||
const api = HttpApi.make("api").add( | ||
HttpApiGroup.make("group").add( | ||
HttpApiEndpoint.get("get", "/").addSuccess(HttpApiSchema.Text()) | ||
) | ||
) | ||
|
||
const spec = OpenApi.fromApi(api) | ||
|
||
console.log(JSON.stringify(spec.paths, null, 2)) | ||
/* | ||
Output: | ||
{ | ||
"/": { | ||
"get": { | ||
"tags": [ | ||
"group" | ||
], | ||
"operationId": "group.get", | ||
"parameters": [], | ||
"security": [], | ||
"responses": { | ||
"200": { | ||
"description": "a string", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
}, | ||
"400": { | ||
"description": "The request did not match the expected schema", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/HttpApiDecodeError" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
*/ | ||
``` | ||
|
||
After | ||
|
||
```diff | ||
import { | ||
HttpApi, | ||
HttpApiEndpoint, | ||
HttpApiGroup, | ||
HttpApiSchema, | ||
OpenApi | ||
} from "@effect/platform" | ||
|
||
const api = HttpApi.make("api").add( | ||
HttpApiGroup.make("group").add( | ||
HttpApiEndpoint.get("get", "/").addSuccess(HttpApiSchema.Text()) | ||
) | ||
) | ||
|
||
const spec = OpenApi.fromApi(api) | ||
|
||
console.log(JSON.stringify(spec.paths, null, 2)) | ||
/* | ||
Output: | ||
{ | ||
"/": { | ||
"get": { | ||
"tags": [ | ||
"group" | ||
], | ||
"operationId": "group.get", | ||
"parameters": [], | ||
"security": [], | ||
"responses": { | ||
"200": { | ||
"description": "a string", | ||
"content": { | ||
- "application/json": { | ||
+ "text/plain": { | ||
"schema": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
}, | ||
"400": { | ||
"description": "The request did not match the expected schema", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/HttpApiDecodeError" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
*/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@effect/platform": patch | ||
--- | ||
|
||
Add missing `deprecated` key to `OpenApi.annotations` API. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
--- | ||
"@effect/platform": patch | ||
--- | ||
|
||
Fix: Prevent request body from being added to the OpenAPI spec for GET methods in `OpenApi.fromApi`. | ||
|
||
When creating a `GET` endpoint with a request payload, the `requestBody` was incorrectly added to the OpenAPI specification, which is invalid for `GET` methods. | ||
|
||
Before | ||
|
||
```ts | ||
import { | ||
HttpApi, | ||
HttpApiEndpoint, | ||
HttpApiGroup, | ||
OpenApi | ||
} from "@effect/platform" | ||
import { Schema } from "effect" | ||
|
||
const api = HttpApi.make("api").add( | ||
HttpApiGroup.make("group").add( | ||
HttpApiEndpoint.get("get", "/") | ||
.addSuccess(Schema.String) | ||
.setPayload( | ||
Schema.Struct({ | ||
a: Schema.String | ||
}) | ||
) | ||
) | ||
) | ||
|
||
const spec = OpenApi.fromApi(api) | ||
|
||
console.log(JSON.stringify(spec.paths, null, 2)) | ||
/* | ||
Output: | ||
{ | ||
"/": { | ||
"get": { | ||
"tags": [ | ||
"group" | ||
], | ||
"operationId": "group.get", | ||
"parameters": [ | ||
{ | ||
"name": "a", | ||
"in": "query", | ||
"schema": { | ||
"type": "string" | ||
}, | ||
"required": true | ||
} | ||
], | ||
"security": [], | ||
"responses": { | ||
"200": { | ||
"description": "a string", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
}, | ||
"400": { | ||
"description": "The request did not match the expected schema", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/HttpApiDecodeError" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"requestBody": { | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "object", | ||
"required": [ | ||
"a" | ||
], | ||
"properties": { | ||
"a": { | ||
"type": "string" | ||
} | ||
}, | ||
"additionalProperties": false | ||
} | ||
} | ||
}, | ||
"required": true | ||
} | ||
} | ||
} | ||
} | ||
*/ | ||
``` | ||
|
||
After | ||
|
||
```ts | ||
import { | ||
HttpApi, | ||
HttpApiEndpoint, | ||
HttpApiGroup, | ||
OpenApi | ||
} from "@effect/platform" | ||
import { Schema } from "effect" | ||
|
||
const api = HttpApi.make("api").add( | ||
HttpApiGroup.make("group").add( | ||
HttpApiEndpoint.get("get", "/") | ||
.addSuccess(Schema.String) | ||
.setPayload( | ||
Schema.Struct({ | ||
a: Schema.String | ||
}) | ||
) | ||
) | ||
) | ||
|
||
const spec = OpenApi.fromApi(api) | ||
|
||
console.log(JSON.stringify(spec.paths, null, 2)) | ||
/* | ||
Output: | ||
{ | ||
"/": { | ||
"get": { | ||
"tags": [ | ||
"group" | ||
], | ||
"operationId": "group.get", | ||
"parameters": [ | ||
{ | ||
"name": "a", | ||
"in": "query", | ||
"schema": { | ||
"type": "string" | ||
}, | ||
"required": true | ||
} | ||
], | ||
"security": [], | ||
"responses": { | ||
"200": { | ||
"description": "a string", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
}, | ||
"400": { | ||
"description": "The request did not match the expected schema", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/HttpApiDecodeError" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
*/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
--- | ||
"@effect/platform": patch | ||
--- | ||
|
||
Add `"application/x-www-form-urlencoded"` to `OpenApiSpecContentType` type as it is generated by the system when using `HttpApiSchema.withEncoding({ kind: "UrlParams" })` | ||
|
||
**Example** | ||
|
||
```ts | ||
import { | ||
HttpApi, | ||
HttpApiEndpoint, | ||
HttpApiGroup, | ||
HttpApiSchema, | ||
OpenApi | ||
} from "@effect/platform" | ||
import { Schema } from "effect" | ||
|
||
const api = HttpApi.make("api").add( | ||
HttpApiGroup.make("group").add( | ||
HttpApiEndpoint.post("post", "/") | ||
.addSuccess(Schema.String) | ||
.setPayload( | ||
Schema.Struct({ foo: Schema.String }).pipe( | ||
HttpApiSchema.withEncoding({ kind: "UrlParams" }) | ||
) | ||
) | ||
) | ||
) | ||
|
||
const spec = OpenApi.fromApi(api) | ||
|
||
console.log(JSON.stringify(spec.paths, null, 2)) | ||
/* | ||
Output: | ||
{ | ||
"/": { | ||
"post": { | ||
"tags": [ | ||
"group" | ||
], | ||
"operationId": "group.post", | ||
"parameters": [], | ||
"security": [], | ||
"responses": { | ||
"200": { | ||
"description": "a string", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
}, | ||
"400": { | ||
"description": "The request did not match the expected schema", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/HttpApiDecodeError" | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"requestBody": { | ||
"content": { | ||
"application/x-www-form-urlencoded": { | ||
"schema": { | ||
"type": "object", | ||
"required": [ | ||
"foo" | ||
], | ||
"properties": { | ||
"foo": { | ||
"type": "string" | ||
} | ||
}, | ||
"additionalProperties": false | ||
} | ||
} | ||
}, | ||
"required": true | ||
} | ||
} | ||
} | ||
} | ||
*/ | ||
``` |
Oops, something went wrong.