-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): support
contentType
on multipart requests in the fern de…
…finition (#4651)
- Loading branch information
Showing
89 changed files
with
2,877 additions
and
167 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
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
12 changes: 12 additions & 0 deletions
12
packages/cli/fern-definition/schema/src/schemas/HttpInlineRequestBodyPropertySchema.ts
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,12 @@ | ||
import { z } from "zod"; | ||
import { DeclarationWithNameSchema } from "./DeclarationWithNameSchema"; | ||
import { extendTypeReferenceSchema } from "./TypeReferenceSchema"; | ||
|
||
export const HttpInlineRequestBodyPropertySchema = extendTypeReferenceSchema( | ||
DeclarationWithNameSchema.extend({ | ||
// For multipart form uploads | ||
["content-type"]: z.string().optional() | ||
}).shape | ||
); | ||
|
||
export type HttpInlineRequestBodyPropertySchema = z.infer<typeof HttpInlineRequestBodyPropertySchema>; |
5 changes: 3 additions & 2 deletions
5
packages/cli/fern-definition/schema/src/schemas/HttpInlineRequestBodySchema.ts
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
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
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
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
16 changes: 16 additions & 0 deletions
16
...pe-only-for-multipart/__test__/__snapshots__/content-type-only-for-multipart.test.ts.snap
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,16 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`compatible-ir-versions > simple failure 1`] = ` | ||
[ | ||
{ | ||
"message": "baz has content-type, but the request is not multipart", | ||
"nodePath": [ | ||
"service", | ||
"endpoints", | ||
"json", | ||
], | ||
"relativeFilepath": "simple.yml", | ||
"severity": "error", | ||
}, | ||
] | ||
`; |
18 changes: 18 additions & 0 deletions
18
...rc/rules/content-type-only-for-multipart/__test__/content-type-only-for-multipart.test.ts
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,18 @@ | ||
import { AbsoluteFilePath, join, RelativeFilePath } from "@fern-api/fs-utils"; | ||
import { getViolationsForRule } from "../../../testing-utils/getViolationsForRule"; | ||
import { ContentTypeOnlyForMultipartRule } from "../content-type-only-for-multipart"; | ||
|
||
describe("compatible-ir-versions", () => { | ||
it("simple failure", async () => { | ||
const violations = await getViolationsForRule({ | ||
rule: ContentTypeOnlyForMultipartRule, | ||
absolutePathToWorkspace: join( | ||
AbsoluteFilePath.of(__dirname), | ||
RelativeFilePath.of("fixtures"), | ||
RelativeFilePath.of("simple") | ||
) | ||
}); | ||
|
||
expect(violations).toMatchSnapshot(); | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
...tor/src/rules/content-type-only-for-multipart/__test__/fixtures/simple/definition/api.yml
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 @@ | ||
name: simple-api |
27 changes: 27 additions & 0 deletions
27
.../src/rules/content-type-only-for-multipart/__test__/fixtures/simple/definition/simple.yml
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,27 @@ | ||
service: | ||
base-path: / | ||
auth: false | ||
endpoints: | ||
json: | ||
path: "" | ||
method: POST | ||
request: | ||
name: ExampleReq | ||
body: | ||
properties: | ||
foo: integer | ||
baz: | ||
type: string | ||
content-type: "application/json" | ||
|
||
multipart: | ||
path: "" | ||
method: PUT | ||
request: | ||
name: MultipartReq | ||
body: | ||
properties: | ||
foo: file | ||
baz: | ||
type: string | ||
content-type: "application/json" |
16 changes: 16 additions & 0 deletions
16
...lidator/src/rules/content-type-only-for-multipart/__test__/fixtures/simple/generators.yml
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,16 @@ | ||
groups: | ||
python-sdk: | ||
audiences: | ||
- external | ||
generators: | ||
- name: fernapi/fern-python-sdk | ||
version: 2.0.0 | ||
output: | ||
location: pypi | ||
package-name: fern-api | ||
token: ${PYPI_TOKEN} | ||
github: | ||
repository: fern-api/python-sdk | ||
license: MIT | ||
config: | ||
client_class_name: Fern |
47 changes: 47 additions & 0 deletions
47
...on/validator/src/rules/content-type-only-for-multipart/content-type-only-for-multipart.ts
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,47 @@ | ||
import { parseFileUploadRequest, isInlineRequestBody } from "@fern-api/fern-definition-schema"; | ||
import { Rule, RuleViolation } from "../../Rule"; | ||
|
||
export const ContentTypeOnlyForMultipartRule: Rule = { | ||
name: "content-type-only-for-multipart", | ||
DISABLE_RULE: false, | ||
create: () => { | ||
return { | ||
definitionFile: { | ||
httpEndpoint: ({ endpoint }) => { | ||
if (endpoint.request == null) { | ||
return []; | ||
} | ||
|
||
const parsedFileUploadRequest = parseFileUploadRequest(endpoint.request); | ||
if (parsedFileUploadRequest != null) { | ||
return []; | ||
} | ||
|
||
if ( | ||
typeof endpoint.request !== "string" && | ||
endpoint.request.body != null && | ||
isInlineRequestBody(endpoint.request.body) | ||
) { | ||
const violations: RuleViolation[] = []; | ||
for (const [propertyName, propertyDeclaration] of Object.entries( | ||
endpoint.request.body.properties ?? {} | ||
)) { | ||
if (typeof propertyDeclaration === "string") { | ||
continue; | ||
} | ||
if (propertyDeclaration["content-type"] != null) { | ||
violations.push({ | ||
severity: "error", | ||
message: `${propertyName} has content-type, but the request is not multipart` | ||
}); | ||
} | ||
} | ||
return violations; | ||
} | ||
|
||
return []; | ||
} | ||
} | ||
}; | ||
} | ||
}; |
1 change: 1 addition & 0 deletions
1
packages/cli/fern-definition/validator/src/rules/content-type-only-for-multipart/index.ts
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 @@ | ||
export { ContentTypeOnlyForMultipartRule } from "./content-type-only-for-multipart"; |
Oops, something went wrong.