trpc-openapi icon indicating copy to clipboard operation
trpc-openapi copied to clipboard

Input / output missing parameters in openapi.json

Open sokactomas opened this issue 2 years ago • 4 comments

Hi,

hi I'm using openapi docs as input for ASM security. but the output from openapi does not correspond to the endpoints that are called within the application

trpc.record.find.useQuery({ id: 123 }) // called url /api/trpc/record.find?=input={"id":123}

this does not match the openapi docs

"/record.find": {
      "get": {
        "operationId": "query.record.find",
        "parameters": [
          {
            "name": "id",
            "in": "query",
            "required": true,
            "schema": {
              "type": "number"
            }
          }
        ],
    }
}

according to the trpc.io documentation

input: https://trpc.io/docs/rpc#methods---type-mapping output: https://trpc.io/docs/rpc#successful-response

these parameters are not in the openapi json, I cannot use this output when verifying requests and responses

is this problem solvable? Thanks

sokactomas avatar Mar 13 '23 11:03 sokactomas

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 12 '23 18:05 stale[bot]

I'm experiencing the same issue, any update on this?

bpneal avatar May 19 '23 13:05 bpneal

My workaround

utils/openapi.ts

import { OpenAPIV3 } from 'openapi-types';
import PathsObject = OpenAPIV3.PathsObject;
import ParameterObject = OpenAPIV3.ParameterObject;

export function getModifiedOpenApiDocs(openApiDocument: OpenAPIV3.Document) {
    const newOpenApiDocument: OpenAPIV3.Document = { ...openApiDocument };
    const newPaths: PathsObject<{}, {}> = {};
    Object.entries(openApiDocument?.paths)?.map(([key, value]) => {
        newPaths[key] = { ...value };

        if (value.get) {
            const properties = {};
            value.get.parameters?.map((parameter: ParameterObject) => {
                properties[parameter.name] = {
                    ...parameter.schema,
                    description: parameter.description,
                };
            });
            if (Object.values(properties).length > 0) {
                newPaths[key].get.parameters = [
                    {
                        name: 'input',
                        in: 'query',
                        content: {
                            'application/json': {
                                schema: {
                                    type: 'object',
                                    properties: properties,
                                },
                            },
                        },
                    },
                ];
            } else {
                newPaths[key].get.parameters = [];
            }
        }
    });

    newOpenApiDocument.paths = newPaths;
    return newOpenApiDocument;
}

Usage

export const openApiDocument = getModifiedOpenApiDocs(
    generateOpenApiDocument(appRouter, {
        title: 'Title',
        version: '1.0.0',
        baseUrl: 'your_url'
    })
);

sokactomas avatar May 19 '23 13:05 sokactomas

I don't understand the issue, could you clarify & provide a reproduction?

I think you might have misunderstood that trpc-openapi is a server-side adapter for your tRPC routers & it does not change the behavior of @trpc/client?

jlalmes avatar May 24 '23 04:05 jlalmes