question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to express an "any" type for a property.

See original GitHub issue

I’m trying to work out how to express a property that’s an “any” type using swagger and autorest. What I found through searching the web is that in swagger 2.0 you need to simply remove the “type” for a property to say its an any which if you look at my value property you’ll see I’ve done:

"CardAction": {
      "description": "A clickable action",
      "type": "object",
      "properties": {
        "type": {
          "$ref": "#/definitions/ActionTypes",
          "description": "The type of action implemented by this button"
        },
        "title": {
          "description": "Text description which appears on the button",
          "type": "string"
        },
        "image": {
          "description": "Image URL which will appear on the button, next to text label",
          "type": "string"
        },
        "text": {
          "description": "Text for this action",
          "type": "string"
        },
        "displayText": {
          "description": "(Optional) text to display in the chat feed if the button is clicked",
          "type": "string"
        },
        "value": {
          "description": "Supplementary parameter for action. Content of this property depends on the ActionType"
        }
      }
    }

But when I run this through autorest using the --typescript switch get a mapping to type object in the generated mapper.ts file which causes the generated client to fail the outbound serialization for non-object values:

export const CardAction = {
  required: false,
  serializedName: 'CardAction',
  type: {
    name: 'Composite',
    className: 'CardAction',
    modelProperties: {
      type: {
        required: false,
        serializedName: 'type',
        type: {
          name: 'String'
        }
      },
      title: {
        required: false,
        serializedName: 'title',
        type: {
          name: 'String'
        }
      },
      image: {
        required: false,
        serializedName: 'image',
        type: {
          name: 'String'
        }
      },
      text: {
        required: false,
        serializedName: 'text',
        type: {
          name: 'String'
        }
      },
      displayText: {
        required: false,
        serializedName: 'displayText',
        type: {
          name: 'String'
        }
      },
      value: {
        required: false,
        serializedName: 'value',
        type: {
          name: 'Object'
        }
      }
    }
  }
};

Is this just a bug in the typescript plugin?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Steveniccommented, Feb 23, 2018

Awesome! Thanks @olydis. We already have a post processing script to do other similar fixups so I should be good. Thanks again for the quick turn around 😃

1reaction
olydiscommented, Feb 23, 2018

@Stevenic The legendary @amarzavery has spoken: He’ll update ms-rest-js (should be tagged here soon) to allow you to write

      value: {
        required: false,
        serializedName: 'value',
        type: {
          name: 'Any'    // <======= new thing here
        }
      }

Long term, this will be what AutoRest generates for your Swagger, but well, you know about the issue tracking that 😉 Until then, I’ll happily assist you to hack something into your generation command that’ll replace Object with Any for that property, in case manually changing that is not feasible (e.g. if you expect to regen very frequently, which would always override your fix…)

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript interface require one of two properties to exist
I've been playing with this example so I can better understand it. It appears that if you give component and click a type...
Read more >
[@types/express] Latest types throw Property 'headers', 'body ...
I tried using the @types/express package and had problems. I tried using the latest stable ... Property 'headers' does not exist on type...
Read more >
Documentation - Everyday Types - TypeScript
In this chapter, we'll cover some of the most common types of values you'll find in JavaScript code, and explain the corresponding ways...
Read more >
How To Create Custom Types in TypeScript - DigitalOcean
In this section, you are going create types that can be used to describe any object shape you need to use in your...
Read more >
5 Common Methods of Holding Real Property Title
There are different ways, all determined by state law, for an individual to ... real estate is a type of property that's made...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found