Skip to content

Commit

Permalink
fix(cli): support 204 status code with no type (#5781)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Jan 30, 2025
1 parent f490c33 commit 1892486
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 44 deletions.
12 changes: 8 additions & 4 deletions fern.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2000,7 +2000,14 @@
]
},
"type": {
"type": "string"
"oneOf": [
{
"type": "string"
},
{
"type": "null"
}
]
},
"property": {
"oneOf": [
Expand All @@ -2023,9 +2030,6 @@
]
}
},
"required": [
"type"
],
"additionalProperties": false
},
"service.HttpResponseSchema": {
Expand Down
2 changes: 1 addition & 1 deletion fern/apis/fern-definition/definition/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ types:
extends:
- commons.WithDocsSchema
properties:
type: string
type: optional<string>
property: optional<string>
status-code: optional<integer>

Expand Down
16 changes: 16 additions & 0 deletions fern/pages/changelogs/cli/2025-01-30.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## 0.51.12
**`(fix):`** The Fern Definition now allows you to declare status codes for the response without having a type.
This is useful for `204` response status codes.

```yml users.yml
service:
auth: false
base-path: /users
endpoints:
update:
path: ""
response:
status-code: 204
```
3 changes: 3 additions & 0 deletions fern/pages/changelogs/ts-express/2025-01-30.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.17.7
**`(internal):`** Upgrade to IRv55.

12 changes: 8 additions & 4 deletions package-yml.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2020,7 +2020,14 @@
]
},
"type": {
"type": "string"
"oneOf": [
{
"type": "string"
},
{
"type": "null"
}
]
},
"property": {
"oneOf": [
Expand All @@ -2043,9 +2050,6 @@
]
}
},
"required": [
"type"
],
"additionalProperties": false
},
"service.HttpResponseSchema": {
Expand Down
20 changes: 20 additions & 0 deletions packages/cli/cli/versions.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
- changelogEntry:
- summary: |
The Fern Definition now allows you to declare status codes for the response without having a type.
This is useful for `204` response status codes.
```yml users.yml
service:
auth: false
base-path: /users
endpoints:
update:
path: ""
response:
status-code: 204
```
type: fix
irVersion: 55
version: 0.51.12


- changelogEntry:
- summary: |
The OpenAPI parser generates response examples that are `{}` for 204 response types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import * as FernDefinition from "../../../index";

export interface HttpResponseSchemaDetailed extends FernDefinition.WithDocsSchema {
type: string;
type?: string;
property?: string;
"status-code"?: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ export const HttpResponseSchemaDetailed: core.serialization.ObjectSchema<
FernDefinition.HttpResponseSchemaDetailed
> = core.serialization
.object({
type: core.serialization.string(),
type: core.serialization.string().optional(),
property: core.serialization.string().optional(),
"status-code": core.serialization.number().optional(),
})
.extend(WithDocsSchema);

export declare namespace HttpResponseSchemaDetailed {
export interface Raw extends WithDocsSchema.Raw {
type: string;
type?: string | null;
property?: string | null;
"status-code"?: number | null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,11 @@ function visitEndpoint({
visitObject(response, {
docs: createDocsVisitor(visitor, nodePathForResponse),
type: (type) => {
visitTypeReference(type, [...nodePathForResponse, "type"], {
location: TypeReferenceLocation.Response
});
if (type != null) {
visitTypeReference(type, [...nodePathForResponse, "type"], {
location: TypeReferenceLocation.Response
});
}
},
property: noop,
"status-code": noop
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export const NoResponsePropertyRule: Rule = {
return [];
}
const responseType = typeof response === "string" ? response : response.type;
if (responseType == null) {
return [];
}
if (parseRawFileType(responseType) != null) {
return [];
} else if (parseRawTextType(responseType) != null) {
Expand All @@ -38,8 +41,12 @@ export const NoResponsePropertyRule: Rule = {
rootApiFile: workspace.definition.rootApiFile.contents,
casingsGenerator: CASINGS_GENERATOR
});
const responseTypeReference = typeof response !== "string" ? response.type : response;
if (responseTypeReference == null) {
return [];
}
const resolvedType = typeResolver.resolveTypeOrThrow({
type: typeof response !== "string" ? response.type : response,
type: responseTypeReference,
file
});
const result = resolvedTypeHasProperty(resolvedType, responseProperty, file, typeResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,14 @@ function validateBodyResponse({
const violations: RuleViolation[] = [];
if (example.error == null) {
if (endpoint.response != null) {
const responseTypeReference =
typeof endpoint.response !== "string" ? endpoint.response.type : endpoint.response;
if (responseTypeReference == null) {
return violations;
}
violations.push(
...ExampleValidators.validateTypeReferenceExample({
rawTypeReference:
typeof endpoint.response !== "string" ? endpoint.response.type : endpoint.response,
rawTypeReference: responseTypeReference,
example: example.body,
typeResolver,
exampleResolver,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,22 @@ export function convertNonStreamHttpResponseBody({
const docs = typeof response !== "string" ? response.docs : undefined;
const responseType = typeof response === "string" ? response : response.type;

if (parseRawFileType(responseType) != null) {
return HttpResponseBody.fileDownload({
docs
});
} else if (parseRawTextType(responseType) != null) {
return HttpResponseBody.text({
docs
});
} else if (parseRawBytesType(responseType) != null) {
return HttpResponseBody.bytes({
docs
});
} else {
return convertJsonResponse(response, docs, file, typeResolver);
if (responseType != null) {
if (parseRawFileType(responseType) != null) {
return HttpResponseBody.fileDownload({
docs
});
} else if (parseRawTextType(responseType) != null) {
return HttpResponseBody.text({
docs
});
} else if (parseRawBytesType(responseType) != null) {
return HttpResponseBody.bytes({
docs
});
} else {
return convertJsonResponse(response, docs, file, typeResolver);
}
}
}

Expand Down Expand Up @@ -173,10 +175,16 @@ function convertJsonResponse(
docs: string | undefined,
file: FernFileContext,
typeResolver: TypeResolver
): HttpResponseBody.Json {
const responseBodyType = file.parseTypeReference(response);
): HttpResponseBody.Json | undefined {
const responseTypeReference = typeof response !== "string" ? response.type : response;
if (responseTypeReference == null) {
return undefined;
}
const responseBodyType = file.parseTypeReference(
typeof response === "string" ? response : { ...response, type: responseTypeReference }
);
const resolvedType = typeResolver.resolveTypeOrThrow({
type: typeof response !== "string" ? response.type : response,
type: responseTypeReference,
file
});
const responseProperty = typeof response !== "string" ? response.property : undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { generateEndpointExample } from "@fern-api/ir-generator";
import {
AutogeneratedEndpointExample,
ExampleCodeSample,
IntermediateRepresentation,
FernIr as Ir,
UserSpecifiedEndpointExample
} from "@fern-api/ir-sdk";
Expand Down Expand Up @@ -563,14 +564,20 @@ function convertResponse(irResponse: Ir.http.HttpResponse): FdrCjsSdk.api.v1.reg
);
if (type != null) {
return { type, statusCode: irResponse.statusCode, description };
} else if (irResponse.statusCode != null) {
return {
statusCode: irResponse.statusCode,
description,
type: { type: "object", extends: [], properties: [], extraProperties: undefined }
};
} else {
return undefined;
}
}

function convertResponseErrorsV2(
irResponseErrors: Ir.http.ResponseErrors,
ir: Ir.ir.IntermediateRepresentation
ir: IntermediateRepresentation
): FdrCjsSdk.api.v1.register.ErrorDeclarationV2[] {
const errors: FdrCjsSdk.api.v1.register.ErrorDeclarationV2[] = [];
if (ir.errorDiscriminationStrategy.type === "statusCode") {
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/workspace/lazy-fern-workspace/src/fern.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2000,7 +2000,14 @@
]
},
"type": {
"type": "string"
"oneOf": [
{
"type": "string"
},
{
"type": "null"
}
]
},
"property": {
"oneOf": [
Expand All @@ -2023,9 +2030,6 @@
]
}
},
"required": [
"type"
],
"additionalProperties": false
},
"service.HttpResponseSchema": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2020,7 +2020,14 @@
]
},
"type": {
"type": "string"
"oneOf": [
{
"type": "string"
},
{
"type": "null"
}
]
},
"property": {
"oneOf": [
Expand All @@ -2043,9 +2050,6 @@
]
}
},
"required": [
"type"
],
"additionalProperties": false
},
"service.HttpResponseSchema": {
Expand Down

0 comments on commit 1892486

Please sign in to comment.