Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenApi fails to use a model with a property named Aliases #167

Closed
IamLeandrooooo opened this issue Jul 8, 2021 · 13 comments · Fixed by #218
Closed

OpenApi fails to use a model with a property named Aliases #167

IamLeandrooooo opened this issue Jul 8, 2021 · 13 comments · Fixed by #218
Assignees
Labels
bug Something isn't working oca-issue For participants from OSS Contribution Academy question Further information is requested v0.9.0

Comments

@IamLeandrooooo
Copy link

IamLeandrooooo commented Jul 8, 2021

Hi guys, so, I have this model in my solution:

public class WeatherForecast
   {
       public DateTime Date { get; set; }

       public int TemperatureC { get; set; }

       public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

       public string Summary { get; set; }

       public List<Aliases> Aliases { get; set; }
   }

   public class Aliases
   {
       public DateTime Date { get; set; }

      
   }
}

If I try to add this property to the Function:
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "text/plain", bodyType: typeof(List<WeatherForecast>), Summary = "The response", Description = "This returns a weatherList")]

And if I try to check the Function Swagger, I get this error:
image

If I change the name, the error disappears.
For example, If I change the property name to Alias I have no problem, but if I make it Aliases it just gives me that error.

Can someone confirm whether or not this word is somehow reserved or is being used in the code and it's giving this error?

I don't know if this is actually related to this, but the azure functions open API extension solution has these 2 js Swagger Files that might be excluding the name:

image

image

@justinyoo
Copy link
Contributor

That's interesting. Can you change the class name from Aliases to BlackWidows and property name to BlackWidows, and see how it's going?

NOTE: BlackWidows is just an arbitrary name in a plural form. You can change it to Skywalkers, if you like.

Try this (class name with s):

public class BlackWidows
{
    public DateTime Date { get; set; }
}

public class WeatherForecast
{
    ...
    public List<BlackWidows> BlackWidows { get; set; }
}

And try this (class name without s):

public class BlackWidow
{
    public DateTime Date { get; set; }
}

public class WeatherForecast
{
    ...
    public List<BlackWidow> BlackWidows { get; set; }
}

@justinyoo justinyoo added question Further information is requested triage labels Jul 9, 2021
@IamLeandrooooo
Copy link
Author

BlackWidows 

So I tried that and indeed failed.

It seems like when the Property has the same name as the class, it fails.

But, as far as I can confirm, it only happens for objects of List, because I have a Property in my main project with the same name of the model but it is like this:

public Class Class { get; set; }

and it works.

The moment I change it to List Class it fails with the Swagger Error.

@justinyoo
Copy link
Contributor

Thanks for the confirmation. Let me have a look.

@justinyoo justinyoo added investigating Need time to investigating and removed triage labels Jul 12, 2021
@IamLeandrooooo
Copy link
Author

Thanks for the confirmation. Let me have a look.

Thank you very much!
When you have news about it, let me know! 😁

@kharnt0x
Copy link

kharnt0x commented Aug 11, 2021

Also seeing the same issue

@level120
Copy link
Contributor

level120 commented Aug 27, 2021

That's interesting! I will try this issue, but i'm not sure that solve or not.

@github-actions github-actions bot added the oca-issue For participants from OSS Contribution Academy label Aug 27, 2021
@justinyoo
Copy link
Contributor

@level120 You want to try to take this issue? This will be a breaking change. Let's keep this discussion open and going.

@level120
Copy link
Contributor

level120 commented Aug 27, 2021

@justinyoo Sure, It is a good chance for me.

@justinyoo
Copy link
Contributor

@level120 Because we don't know what will be duplicated, appending sequence number could be a good idea. For example,

public class BlackWidows
{
    public DateTime Date { get; set; }
}

public class WeatherForecast
{
    ...
    public List<BlackWidows> BlackWidows { get; set; }
}

It should be something like:

{
  "components": {
    "schemas": {
      "BlackWidows": {
        "type": "object",
        "properties": {
          "date": {
            ...
          }
        }
      },
      "BlackWidows_1": {
        "type": "array",
        "properties": {
          "items": {
            "$ref": "#/components/schemas/BlackWidows"
          }
        }
      },
      "WeatherForecast": {
        "type": "object",
        "properties": {
          "blackWidows": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/BlackWidows_1"
            }
          }
        }
      }
    }
  }
}

@level120
Copy link
Contributor

@justinyoo How about this? I think that its aliases have a different reference.

{
  "components": {
    "schemas": {
      "blackWidows": {
        "type": "object",
        "properties": {
          "date": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "weatherForecast": {
        "type": "object",
        "properties": {
          "blackWidows": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/blackWidows"
            }
          }
        }
      }
    }
  }
}

The "blackWindows" reference is "#/components/schemas/blackWindows".
The "weatherForecast.blackWindows" reference is "#/components/schemas/weatherForcast/properties/blackWindows".

@justinyoo
Copy link
Contributor

@level120 The expected JSON object should be like that, as you mentioned.

{
  "components": {
    "schemas": {
      "blackWidows": {
        "type": "object",
        "properties": {
          "date": {
            "type": "string",
            "format": "date-time"
          }
        }
      },
      "weatherForecast": {
        "type": "object",
        "properties": {
          "blackWidows": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/blackWidows"
            }
          }
        }
      }
    }
  }
}

The current issue is that it's not rendered like that, if there's a property that has a same name of the schema object.

@level120
Copy link
Contributor

@justinyoo In my opinion, When instance schema key check skip, it makes the correct script.
I think that subAcceptor having schemas should not check if key same with instance schemas.

image

@justinyoo
Copy link
Contributor

@level120 That might be correct. But make sure that the schema only allows the unique name. Can you write relevant codes including test codes that prove your idea?

@justinyoo justinyoo added bug Something isn't working v0.9.0 working and removed investigating Need time to investigating labels Sep 7, 2021
@justinyoo justinyoo removed the working label Oct 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working oca-issue For participants from OSS Contribution Academy question Further information is requested v0.9.0
Projects
None yet
4 participants