Giter Site home page Giter Site logo

Comments (8)

Adamantinu avatar Adamantinu commented on August 27, 2024 2

Would be great to be fixed since the .net core feature of the ProblemDetails runs into this problem when documented via the ProducesResponseTypeAttribute

from openapi-typescript.

cjpete avatar cjpete commented on August 27, 2024

Here's a patchfile for anyone else who needs this. I'll put a PR together too.

diff --git a/node_modules/@manifoldco/swagger-to-ts/dist-node/index.js b/node_modules/@manifoldco/swagger-to-ts/dist-node/index.js
index ff14209..3f5f525 100644
--- a/node_modules/@manifoldco/swagger-to-ts/dist-node/index.js
+++ b/node_modules/@manifoldco/swagger-to-ts/dist-node/index.js
@@ -216,7 +216,17 @@ function generateTypesV2(schema, options) {
           let properties = createKeys(node.properties || {}, node.required);
 
           if (node.additionalProperties) {
-            properties += `[key: string]: ${nodeType(node.additionalProperties) || "any"};\n`;
+            const nodeTypeOfAdditionalProps = nodeType(node.additionalProperties)
+            if (nodeTypeOfAdditionalProps === 'array') {
+              const nodeTypeOfAdditionalItems = nodeType(node.additionalProperties.items)
+              if (nodeTypeOfAdditionalItems === 'ref') {
+                properties += `[key: string]: ${tsArrayOf(transform(node.additionalProperties.items))};\n`;
+              } else {
+                properties += `[key: string]: ${nodeTypeOfAdditionalItems || "any"};\n`;
+              }
+            } else {
+              properties += `[key: string]: ${nodeTypeOfAdditionalProps || "any"};\n`;
+            }
           }
 
           return tsIntersectionOf([...(node.allOf ? node.allOf.map(transform) : []), ...(properties ? [`{ ${properties} }`] : [])]);
@@ -307,7 +317,6 @@ function generateTypesV3(schema, options) {
         }
     }
 
-    return "";
   }
 
   function createKeys(obj, required) {

from openapi-typescript.

zach-waggoner avatar zach-waggoner commented on August 27, 2024

I have a similar problem which seems related. Given the schema

components:
  schemas:
    Example:
      type: object
      properties:
        dictionary:
          type: object
          additionalProperties:
            type: number
            nullable: true
      required:
        - dictionary

swagger-to-ts generates

export interface components {
  schemas: {
    Example: {
      dictionary: { [key: string]: number };
    };
  };
}

whereas it should be

export interface components {
  schemas: {
    Example: {
      dictionary: { [key: string]: number | null };
    };
  };
}

from openapi-typescript.

andrew-farries avatar andrew-farries commented on August 27, 2024

Here's another minimal reproduction for the lack of additionalProperties support. Given this part of an API spec:

  "components": {
    "schemas": {
      "ProblemDetails": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "nullable": true
          },
          "title": {
            "type": "string",
            "nullable": true
          },
          "status": {
            "type": "integer",
            "format": "int32",
            "nullable": true
          },
          "detail": {
            "type": "string",
            "nullable": true
          },
          "instance": {
            "type": "string",
            "nullable": true
          }
        },
        "additionalProperties": {
          "type": "object",
          "additionalProperties": false
        }
      }
    }
  }

swagger-to-ts generates:

export interface components {
  schemas: {
    ProblemDetails: {
      type?: string | null;
      title?: string | null;
      status?: number | null;
      detail?: string | null;
      instance?: string | null;
      [key: string]: { [key: string]: any };
    };
  };
}

which has an invalid index signature. The index signature needs to be:

[key: string]: { [key: string]: any } | string | null | number;

from openapi-typescript.

bitsofinfo avatar bitsofinfo commented on August 27, 2024

#324 related?

from openapi-typescript.

bitsofinfo avatar bitsofinfo commented on August 27, 2024

I see this is in #294.... how can I make use of this?

from openapi-typescript.

drwpow avatar drwpow commented on August 27, 2024

Just improved support for additionalProperties in #442 but will be taking a look at this next to see if I missed any cases (more than likely I did)

from openapi-typescript.

drwpow avatar drwpow commented on August 27, 2024

Support for additionalProperties has been greatly improved in this library since this issue was filed. I’m going to close this, but if there are any cases I’ve missed please let me know, either in this issue or by opening a new one with a specific schema to test.

from openapi-typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.