kogosoftwarellc / open-api

A Monorepo of various packages to power OpenAPI in node
MIT License
892 stars 235 forks source link

openapi-types: Fixed OpenAPIV3_1.PathItemObject to accept OpenAPIV3_1… #803

Open AdrianMachado opened 2 years ago

AdrianMachado commented 2 years ago

….OperationObject

Problem

In my project, I was trying to construct a OpenAPIV3_1 object as follows

type OperationObjects = {
  [method in OpenAPIV3_1.HttpMethods]?: OpenAPIV3_1.OperationObject;
};
const summary = "blah";
const description = "blah";
const operationObjects = getOperationObjects(routeConfig, routeData); // Returns OperationObjects
const parameterObjects = getParamsFromPath(path); // Return OpenAPIV3_1.ParameterObject
return {
  ...operationObjects,
  parameters: parameterObjects,
  summary, // string
  description, // string
};

When spreading the object I get the following error: https://pastebin.com/XwXWuavS

Solution

I wouldn't consider myself a savvy typescript guy, but I assume the issue is due to the way the method -> OperationObject gets tacked on as a intersection on both V3 and V3_1. This will probably result in typescript interpreting

{
  [method in HttpMethods]?: OpenAPIV3.OperationObject<T>
} & 
{
  [method in HttpMethods]?: OpenAPIV3_1.OperationObject<T>
}

Which I assume would just boil down to

{
  [method in HttpMethods]?: OpenAPIV3.OperationObject<T>
}

To solve this, I Omit the original mapping of method to OpenAPIV3.OperationObject and add it back as method to OpenAPIV3_1.OperationObject as an intersection at the end. I am not sure if this is the most elegant solution, but it does work.

Sidenote: I also noticed a typescript error on PathsWebhooksComponents which I think I fixed too

AdrianMachado commented 2 years ago

@jsdevel care to take a look?

jsdevel commented 2 years ago

@AdrianMachado please fix the build

jsdevel commented 2 years ago

@AdrianMachado can you fix the build?

AdrianMachado commented 2 years ago

Sorry I forgot about this. I will take a look this weekend

soul-codes commented 1 year ago

I just came across this PR after pulling my hair on the same problem upgrading to OpenAPI 3.1. What can we do to help get this merged @AdrianMachado @jsdevel?

AdrianMachado commented 1 year ago

@soul-codes hey - I haven't really had time to fix this - feel free to take it over