Skip to content

Commit

Permalink
code generator now properly handles $ref in a response
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcelhaney committed Apr 13, 2024
1 parent 5f70d13 commit 175cb40
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
52 changes: 42 additions & 10 deletions openapi-example.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
openapi: 3.0.3
info:
version: 1.0.0
version: 1.0.0
title: Sample API
description: A sample API to illustrate OpenAPI concepts
paths:
Expand All @@ -12,9 +12,8 @@ paths:
description: Successful response
content:
application/json:
schema:
type:
string
schema:
type: string
examples:
no visits:
value: You have not visited anyone yet
Expand All @@ -26,9 +25,8 @@ paths:
description: Successful response
content:
application/json:
schema:
type:
string
schema:
type: string
examples:
hello kitty:
value: >-
Expand All @@ -53,12 +51,46 @@ paths:
type: string
content:
application/json:
schema:
type:
string
schema:
type: string
example: an example string
examples:
hello-example1:
value: Hello, example1
hello-example2:
value: Hello, example2
/path-one:
get:
responses:
"200":
"description": "test"
content:
application/json:
schema:
type: string
"400":
$ref: "#/components/responses/BadRequest"

components:
responses:
BadRequest:
description: The request is malformed and so cannot be processed
content:
application/json:
schema:
$ref: "#/components/schemas/Error"

schemas:
Error:
title: Error
description: A generic error message suitable for 4xx and 5xx responses
type: object
properties:
code:
type: string
description: A machine readable error code
message:
type: string
description: A detailed description of the error
required:
- message
4 changes: 4 additions & 0 deletions src/typescript-generator/operation-type-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export class OperationTypeCoder extends Coder {

// eslint-disable-next-line max-statements
write(script) {
if (this.requirement.isReference) {
return script.importType(this);
}

// eslint-disable-next-line no-param-reassign
script.comments = READ_ONLY_COMMENTS;

Expand Down
12 changes: 11 additions & 1 deletion src/typescript-generator/response-type-coder.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export class ResponseTypeCoder extends Coder {
this.openApi2MediaTypes = openApi2MediaTypes;
}

names() {
return super.names(this.requirement.data.$ref.split("/").at(-1));
}

typeForDefaultStatusCode(listedStatusCodes) {
const definedStatusCodes = listedStatusCodes.filter(
(key) => key !== "default",
Expand Down Expand Up @@ -85,8 +89,14 @@ export class ResponseTypeCoder extends Coder {
return requiredHeaders.length === 0 ? "never" : requiredHeaders.join(" | ");
}

modulePath() {
return `components/${this.requirement.data.$ref.split("/").at(-1)}.ts`;
}

write(script) {
script.importSharedType("ResponseBuilderFactory");
if (this.requirement.isReference) {
return script.importType(this);
}

const text = `{
headers: ${this.printHeaders(script, this.requirement)};
Expand Down

0 comments on commit 175cb40

Please sign in to comment.