Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Resolve parameters ref #283

Merged
merged 2 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. Dates are d

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

#### [v14.2.1](https://github.com/contiamo/restful-react/compare/v14.2.0...v14.2.1)

> 10 July 2020

- Resolve parameters $ref [`#259`](https://github.com/contiamo/restful-react/issues/259)
- Fix `npm-publish-without-cli` CI [`a189eaa`](https://github.com/contiamo/restful-react/commit/a189eaa176e6fb00e50b63f9e762493e3008019e)

#### [v14.2.0](https://github.com/contiamo/restful-react/compare/v14.1.1...v14.2.0)

> 10 July 2020
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "restful-react",
"version": "14.2.0",
"version": "14.2.1",
"description": "A consistent, declarative way of interacting with RESTful backends, featuring code-generation from Swagger and OpenAPI specs",
"keywords": [
"rest",
Expand Down
11 changes: 7 additions & 4 deletions src/scripts/import-open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,13 @@ export const generateRestfulComponent = (
// Retrieve the type of the param for delete verb
const lastParamInTheRouteDefinition =
operation.parameters && lastParamInTheRoute
? (operation.parameters.find(p => {
if (isReference(p)) return false;
return p.name === lastParamInTheRoute;
}) as ParameterObject | undefined) // Reference is not possible
? operation.parameters
.map(p =>
isReference(p)
? (get(schemasComponents, p.$ref.replace("#/components/", "").replace("/", ".")) as ParameterObject)
: p,
)
.find(p => p.name === lastParamInTheRoute)
: { schema: { type: "string" } };

if (!lastParamInTheRouteDefinition) {
Expand Down
67 changes: 67 additions & 0 deletions src/scripts/tests/import-open-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1989,6 +1989,73 @@ describe("scripts/import-open-api", () => {
`);
});

it("should resolve parameters ref", () => {
const operation: OperationObject = {
summary: "Delete use case",
operationId: "deleteUseCase",
tags: ["use-case"],
parameters: [
{
$ref: "#/components/parameters/UseCaseId",
},
],
responses: {
"204": {
description: "Empty response",
},
default: {
description: "unexpected error",
content: {
"application/json": {
schema: { $ref: "#/components/schemas/APIError" },
example: { errors: ["msg1", "msg2"] },
},
},
},
},
};

const schemasComponents: ComponentsObject = {
parameters: {
UseCaseId: {
name: "useCaseId",
in: "path",
description: "Id of the usecase",
required: true,
schema: {
type: "string",
},
},
},
};

expect(generateRestfulComponent(operation, "delete", "/use-cases/{useCaseId}", [], undefined, schemasComponents))
.toMatchInlineSnapshot(`
"
export type DeleteUseCaseProps = Omit<MutateProps<void, APIError, void, string, void>, \\"path\\" | \\"verb\\">;

/**
* Delete use case
*/
export const DeleteUseCase = (props: DeleteUseCaseProps) => (
<Mutate<void, APIError, void, string, void>
verb=\\"DELETE\\"
path={\`/use-cases\`}
{...props}
/>
);

export type UseDeleteUseCaseProps = Omit<UseMutateProps<void, APIError, void, string, void>, \\"path\\" | \\"verb\\">;

/**
* Delete use case
*/
export const useDeleteUseCase = (props: UseDeleteUseCaseProps) => useMutate<void, APIError, void, string, void>(\\"DELETE\\", \`/use-cases\`, props);

"
`);
});

it("should generate a Poll compoment if the `prefer` token is present", () => {
const operation: OperationObject = {
summary: "List all fields for the use case schema",
Expand Down