thiagobustamante / typescript-rest-swagger

Swagger tools for typescript-rest
156 stars 57 forks source link

Partial seems not to be supported #138

Open Loksly opened 3 years ago

Loksly commented 3 years ago

Hi everyone!

I recently realized I cannot use Partial. For example this works:

src/services/index.ts

import { Path, POST } from "typescript-rest";
import { Response } from "typescript-rest-swagger";

export interface Complex {
    fieldC: string;
    fieldD: number;
}

export interface MyBodyDefinition {
    fieldA: string;
    fieldB: Complex;
}

@Path("/api/v1/example")
export class ExampleService {

    @Response<MyBodyDefinition>(200, "Configuration")
    @Path("update")
    @POST
    async update( body: MyBodyDefinition ): Promise<MyBodyDefinition> {
      return Promise.resolve(body);
    }  
}

But if you change MyBodyDefinition to:

export interface MyBodyDefinition {
    fieldA: string;
    fieldB: Partial<Complex>;
}

then the output is:

There was a problem resolving type of 'PartialComplex'.
There was a problem resolving type of 'MyBodyDefinition'.
/tmp/failure/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:267
        throw err;
        ^

TypeError: Cannot read property 'filter' of undefined
    at getModelTypeProperties (/tmp/failure/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:463:47)
    at getModelTypeProperties (/tmp/failure/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:459:15)
    at getReferenceType (/tmp/failure/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:250:26)
    at resolveType (/tmp/failure/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:80:25)
    at /tmp/failure/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:452:23
    at Array.map (<anonymous>)
    at getModelTypeProperties (/tmp/failure/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:418:14)
    at getReferenceType (/tmp/failure/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:250:26)
    at resolveType (/tmp/failure/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:91:25)
    at Object.resolveType (/tmp/failure/node_modules/typescript-rest-swagger/dist/metadata/resolveType.js:60:16)
    at MethodGenerator.generate (/tmp/failure/node_modules/typescript-rest-swagger/dist/metadata/methodGenerator.js:46:34)
    at /tmp/failure/node_modules/typescript-rest-swagger/dist/metadata/controllerGenerator.js:87:58
    at Array.map (<anonymous>)
    at ControllerGenerator.buildMethodsForClass (/tmp/failure/node_modules/typescript-rest-swagger/dist/metadata/controllerGenerator.js:87:14)
    at ControllerGenerator.buildMethods (/tmp/failure/node_modules/typescript-rest-swagger/dist/metadata/controllerGenerator.js:69:43)
    at ControllerGenerator.generate (/tmp/failure/node_modules/typescript-rest-swagger/dist/metadata/controllerGenerator.js:48:27)

For the record, the rest of the files have this content:

package.json:

{
  "name": "failure",
  "version": "0.0.1",
  "scripts": {
    "build": "npm run clean && npm run tsc && npm run swagger && npm run swaggerIsOk",
    "clean": "npx rimraf dist .nyc_output",
    "tsc": "./node_modules/typescript/bin/tsc",
    "swagger": "npx swaggerGen -c ./swagger.config.yml",
    "swaggerIsOk": "grep -q '/api/v1' ./dist/swagger.json"
  },
  "dependencies": {
  },
  "devDependencies": {
    "typescript": "^4.2.3",
    "typescript-rest": "^3.0.2",
    "typescript-rest-swagger": "^1.1.6"
  }
}

swagger.config.yml:

swagger:
  outputDirectory: ./dist
  entryFile: 
    - ./src/services/*
  outputFormat: OpenApi_3
  name: Mock server example
  description: This is a test
  license: Unlicensed
  produces: [application/json]
  securityDefinitions:
    default:
      type: http
      scheme: bearer
      name: Authorization

tsconfig.json

{
  "compilerOptions": {
    "alwaysStrict": true,
    "declaration": true,
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "module": "commonjs",
    "newLine": "LF",
    "noFallthroughCasesInSwitch": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "outDir": "./dist/",
    "removeComments": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "strictBindCallApply": true,
    "strictFunctionTypes": true,
    "strictNullChecks": true,
    "strictPropertyInitialization": true,
    "target": "es6"
  },
  "include": ["./src/**/*.ts"],
  "exclude": ["node_modules"]
}

Thanks for your help.

Avrmaster commented 3 years ago

It seems like none of the build-in types are supported. Same issue for union types (it's even worse - a client fallback to "{}")

aperona-hai commented 3 years ago

Generics and Record type also give and error

viniciusln commented 2 years ago

Same issue here using the Pick type.