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

Problem with optional enum query parameter? #10

Open
whophil opened this issue Mar 27, 2024 · 1 comment
Open

Problem with optional enum query parameter? #10

whophil opened this issue Mar 27, 2024 · 1 comment

Comments

@whophil
Copy link

whophil commented Mar 27, 2024

I am having trouble making calls to the API when the endpoint directly uses an optional enum query parameter.

I have followed this section of the docs, but am running into issues which do not seem to be covered.

I have created a minimal openapi.json as shown below. This API has one endpoint, /data/ which takes one query parameter unit_system, which is allowed to be either "SI" or "FPS" - or it can be null.

openapi.json
{
    "openapi": "3.1.0",
    "info":
    {
        "title": "FastAPI",
        "version": "0.1.0"
    },
    "paths":
    {
        "/data/":
        {
            "get":
            {
                "summary": "Get Data",
                "operationId": "get_data",
                "parameters":
                [
                    {
                        "name": "unit_system",
                        "in": "query",
                        "required": false,
                        "schema":
                        {
                            "anyOf":
                            [
                                {
                                    "$ref": "#/components/schemas/UnitSystem"
                                },
                                {
                                    "type": "null"
                                }
                            ],
                            "title": "Unit System"
                        }
                    }
                ],
                "responses":
                {
                    "200":
                    {
                        "description": "Successful Response",
                        "content":
                        {
                            "application/json":
                            {
                                "schema":
                                {
                                    "type": "string",
                                    "title": "Response Get Data"
                                }
                            }
                        }
                    },
                    "422":
                    {
                        "description": "Validation Error",
                        "content":
                        {
                            "application/json":
                            {
                                "schema":
                                {
                                    "$ref": "#/components/schemas/HTTPValidationError"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components":
    {
        "schemas":
        {
            "HTTPValidationError":
            {
                "properties":
                {
                    "detail":
                    {
                        "items":
                        {
                            "$ref": "#/components/schemas/ValidationError"
                        },
                        "type": "array",
                        "title": "Detail"
                    }
                },
                "type": "object",
                "title": "HTTPValidationError"
            },
            "UnitSystem":
            {
                "type": "string",
                "enum":
                [
                    "SI",
                    "FPS"
                ],
                "title": "UnitSystem"
            },
            "ValidationError":
            {
                "properties":
                {
                    "loc":
                    {
                        "items":
                        {
                            "anyOf":
                            [
                                {
                                    "type": "string"
                                },
                                {
                                    "type": "integer"
                                }
                            ]
                        },
                        "type": "array",
                        "title": "Location"
                    },
                    "msg":
                    {
                        "type": "string",
                        "title": "Message"
                    },
                    "type":
                    {
                        "type": "string",
                        "title": "Error Type"
                    }
                },
                "type": "object",
                "required":
                [
                    "loc",
                    "msg",
                    "type"
                ],
                "title": "ValidationError"
            }
        }
    }
}

I generate the client code via:

npx @openapitools/openapi-generator-cli \
	--custom-generator MATLAB/lib/jar/MATLABClientCodegen-openapi-generator-0.0.1.jar \
	generate \
	-g MATLAB \
	-i http://localhost:8000/openapi.json \
	--package-name exampleclient

This generates the following tree:

+exampleclient
├── +api
│   └── Default.m
├── +models
│   ├── FreeFormObject.m
│   ├── HTTPValidationError.m
│   ├── UnitSystem.m
│   ├── Unit_System.m
│   ├── ValidationError.m
│   └── ValidationError_loc_inner.m
├── BaseClient.m
├── CookieJar.m
├── JSONEnum.m
├── JSONMapper.m
├── JSONMapperMap.m
└── JSONPropertyInfo.m

After this, I run into issues when querying. The output below demonstrates trying to call the endpoint in a number of different ways. All of these fail on the Matlab side before even attempting to communicate with the API.

Am I making a mistake, or is this somehow an edge case that is currently not working?

Errors when attempting to use the generated enum models
>> client = exampleclient.api.Default(serverUri="http://localhost:8000")

client = 

  Default with properties:

              serverUri: [1×1 matlab.net.URI]
            httpOptions: [1×1 matlab.net.http.HTTPOptions]
    preferredAuthMethod: [0×0 string]
            bearerToken: '<unset>'
                 apiKey: '<unset>'
        httpCredentials: '<unset>'
                cookies: [1×1 exampleclient.CookieJar]

>> client.getData(unit_system="SI")
Error using matlab.net.QueryParameter>convert
Unable to convert the value of the query parameter
"unit_system" of type exampleclient.models.Unit_System
to a string.

Error in matlab.net.QueryParameter/string (line 351)
                    values = convert(obj.Name, value, obj.Literal);

Error in matlab.net.URI/string (line 673)
                query = '?' + string(obj.Query);

Error in matlab.net.URI/char (line 696)
            str = string(obj);

Error in matlab.net.http.RequestMessage>getProxySettings (line 2133)
    proxyInfo = matlab.internal.webservices.getProxyInfo(char(uri));

Error in matlab.net.http.RequestMessage/send (line 405)
                    [proxyURI, username, password] = getProxySettings(createURIFromInput(uri));

Error in exampleclient.api.Default/getData (line 175)
            [response, ~, history] = send(request, uri, httpOptions);
 
>> client.getData(unit_system=exampleclient.models.UnitSystem.SI)
Error using exampleclient.api.Default/getData
 client.getData(unit_system=exampleclient.models.UnitSystem.SI)
                            ↑
Invalid value for 'unit_system' argument. Value must be of type exampleclient.models.Unit_System or be convertible
to exampleclient.models.Unit_System.
 
>> client.getData(unit_system=exampleclient.models.Unit_System("SI"))
Error using matlab.net.QueryParameter>convert
Unable to convert the value of the query parameter "unit_system" of type exampleclient.models.Unit_System to a
string.

Error in matlab.net.QueryParameter/string (line 351)
                    values = convert(obj.Name, value, obj.Literal);

Error in matlab.net.URI/string (line 673)
                query = '?' + string(obj.Query);

Error in matlab.net.URI/char (line 696)
            str = string(obj);

Error in matlab.net.http.RequestMessage>getProxySettings (line 2133)
    proxyInfo = matlab.internal.webservices.getProxyInfo(char(uri));

Error in matlab.net.http.RequestMessage/send (line 405)
                    [proxyURI, username, password] = getProxySettings(createURIFromInput(uri));

Error in exampleclient.api.Default/getData (line 175)
            [response, ~, history] = send(request, uri, httpOptions);
@brownemi
Copy link
Member

Hi @whophil

Thanks for the report. I've not been able to replicate this exactly I see a different issue...

Could I ask you to share the output of the npx @openapitools/openapi-generator-cli version command please.

Also we've not qualified the package with 3.1.0 support yet and know of some issues, could you change the spec to use 3.0.0 (line #2 of the spec) and see how that goes as a quick test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants