autorest icon indicating copy to clipboard operation
autorest copied to clipboard

An in body parameter with discriminator does not show up in the method signature

Open smkuls-msft opened this issue 9 years ago • 15 comments

I have an API with the following parameter in the swagger spec -

{
        "name": "resultQuery",
        "in": "body",
        "description": "Query to retrieve results based on the query type.",
        "required": true,
        "schema": {
          "$ref": "#/definitions/ResultQuery"
}

This is the schema for the object -

{
	"ResultQuery": {
		"description": "Query for a particular result.",
		"required": ["queryType"],
		"type": "object",
		"properties": {
			"queryType": {
				"description": "Query type which specifies which result needs to be queried.",
				"enum": ["Enum1", "Enum2"],
				"type": "string",
				"x-ms-enum": {
					"name": "ResultQueryType",
					"modelAsString": false
				}
			}
		},
		"discriminator": "queryType"
	}
}

When I generate the client DLL for this spec, the method signature does not have the ResultQuery. However, It works when I remove the discriminator attribute from the schema of "ResultQuery" but then the method does not support polymorphism.

Any workarounds for this?

smkuls-msft avatar Jan 13 '17 04:01 smkuls-msft

I thought for certain that I'd find a problem in the swagger, but when I looked deeper, I can confirm what you're finding.

That's the strangest thing I've seen in a long time.

Lemme see if I can get this fixed asap.

fearthecowboy avatar Jan 13 '17 17:01 fearthecowboy

Aha!

I see what's going on here.

The property that is used for a polymorphic discriminator doesn't actually create a C# Property, because you never need to see the property itself (in essence, the class of the polymorphic type is the property).

And since the enum isn't used by anything that isn't a polymorphic discriminator, it doesn't get created.

Which really means, you don't have to put an enum (and x-ms-enum) on the polymorphic discriminator, since it never ever gets used.

fearthecowboy avatar Jan 13 '17 18:01 fearthecowboy

@fearthecowboy If the enum doesn't get created and is never used then how will the client DLL handle the case when a derived type of a particular parameter is passed to the method? Are you trying to say that the attribute "x-ms-enum" is causing some issue?

smkuls-msft avatar Jan 16 '17 04:01 smkuls-msft

No, there is a JSON deserializer that does the work to deserialize the object to the right type (it uses the string value of the field). The generated enum is never used for this (the deserializer has to peek inside the payload, get the value of the discriminator, and then deserialize to the correct type based on the value of that. Having it put it into an enum would be a complete waste of time.

fearthecowboy avatar Jan 16 '17 19:01 fearthecowboy

@fearthecowboy We are kind of blocked on this, we have a demo planned at the tech ready for our new service. Can you let us know, by when a fix can be made available in the nightly builds.

parvezah avatar Jan 17 '17 10:01 parvezah

I'm sorry, I don't understand how you're blocked -- The code that's generated should work correctly; why do you need the enum class generated?

fearthecowboy avatar Jan 17 '17 14:01 fearthecowboy

The code is generated correctly (with the QueryResult parameter) only if I remove the discriminator attribute (it fails to generate the correct API even when I change the discriminator type to a string). Anyway, I have added a document filter to not add the discriminator attribute in the swagger spec and add the derived types explicitly. Although, I am not sure if this is the correct way to solve this.

smkuls-msft avatar Jan 17 '17 15:01 smkuls-msft

I don't understand why you need an explicit parameter in the generated class for the discriminator value. The code will do the right thing during serialization/deserialization to ensure that the value is used/placed in the json payload. The discriminator value should not ever need be directly accessible to the end developer.

fearthecowboy avatar Jan 17 '17 15:01 fearthecowboy

@fearthecowboy We understand that the discriminator value shouldn't be directly accessible to the end developer of the client dll. But our problem is that when we have the discriminator defined(in swagger json) on a model(resultQuery) which is a in-body param to our api, signature of that operation in the generated code doesnt have that model(resultQuery) as an input variable. If the method signature doesnt have the parameter which is supposed to be passed in the body to our api call, how would the client dll user pass the body? It is not clear to us on how you are asking us to solve this problem.

parvezah avatar Jan 17 '17 15:01 parvezah

Ah, The real problem here is that AutoRest is over-optimizing. The base class in this case has no fields, and when the generator drops the discriminator property, it eliminates the parameter that it sees as unused (because only subclasses have parameters)

Fixing this today.

fearthecowboy avatar Jan 17 '17 16:01 fearthecowboy

(in the mean time, add an unused parameter in the base class and AutoRest will generate the method signatures correctly for the time being)

fearthecowboy avatar Jan 17 '17 16:01 fearthecowboy

@smkuls-work Did you grab the latest nightly and try it ?

fearthecowboy avatar Jan 25 '17 15:01 fearthecowboy

@fearthecowboy could you confirm this is supposed to be fixed by https://github.com/Azure/autorest/pull/1726 ?

This didn't solve issue #1729, but I'm not sure yet how they are related.

lmazuel avatar Jan 25 '17 18:01 lmazuel

Yes, #1726 should have fixed it. (Fixed it in the test I created :) )

fearthecowboy avatar Jan 25 '17 19:01 fearthecowboy

@fearthecowboy Sorry for responding so late. Yes, the issue seems to be have been fixed. Thanks!

smkuls-msft avatar Jan 31 '17 06:01 smkuls-msft