Haskell-OpenAPI-Code-Generator / Haskell-OpenAPI-Client-Code-Generator

Generate Haskell client code from an OpenAPI 3 specification
47 stars 19 forks source link

Are query-params with the name "type" silently ignored? #56

Closed saurabhnanda closed 2 years ago

saurabhnanda commented 3 years ago

Referring to https://docs.hetzner.cloud/spec.json it seems that GET /placement_groups accepts a query-param called "type", which results in the following error when the generated code is compiled:

hcloud-haskell/codegen/src/HCloud/Operations/GetPlacementGroups.hs:55:505: error:
    • Variable not in scope:
        getPlacementGroupsParametersQueryType
          :: GetPlacementGroupsParameters -> GHC.Base.Maybe a3
    • Perhaps you meant one of these:
        ‘getPlacementGroupsParametersQueryName’ (line 67),
        ‘getPlacementGroupsParametersQuerySort’ (line 71),
        data constructor ‘GetPlacementGroupsParametersQuerySortTyped’ (line 91)
   |
55 |                                                                                                                                                                                                                                                                                                                                                                                                                   HCloud.Common.QueryParameter (Data.Text.pack "type") (Data.Aeson.Types.ToJSON.toJSON Data.Functor.<$> getPlacementGroupsParametersQueryType parameters) (Data.Text.pack "form") GHC.Types.False])
   |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

It seems that the other three query params are there, but the type query-param has been silently skipped:

mkGetPlacementGroupsParameters :: GetPlacementGroupsParameters
mkGetPlacementGroupsParameters = GetPlacementGroupsParameters{getPlacementGroupsParametersQueryLabelSelector = GHC.Maybe.Nothing,
                                                              getPlacementGroupsParametersQueryName = GHC.Maybe.Nothing,
                                                              getPlacementGroupsParametersQuerySort = GHC.Maybe.Nothing}

I'm presuming this is because type is a reserved keyword in Haskell. Would it be possible to make a special case for this because this is so common in APIs? Implicitly rename it to typ or type_?

joel-bach commented 3 years ago

Hi @saurabhnanda ,

To answer your original question: No, this parameter is not ignored based on its name (you should be able to use any Haskell keyword, feel free to open an issue if not).

But you have discovered a different problem which results from the combination of an optional parameter and an enum with only one element. As OpenAPI 3 has no other option to describe a constant value, some specifications (including Stripe which is the main specification this generator is currently used for) define enum values with only one element to effectively create a constant value in a type. To ease the use of those constants the corresponding fields are left out from the generation (but is included in the serialisation) as the only valid value is already known. But for optional fields this behaviour is of course not applicable as there are effectively two valid values.

I created this PR to fix it: https://github.com/Haskell-OpenAPI-Code-Generator/Haskell-OpenAPI-Client-Code-Generator/pull/58

Side note: The OpenAPI specification you sent seems strange to me regarding the type parameter as the only allowed value is "spread" but the description states that it can be used multiple times (which seems a bit silly if really only one value is allowed).

joel-bach commented 2 years ago

Hi @saurabhnanda,

Did the PR fix your problem and can we close this issue?

joel-bach commented 2 years ago

I close this issue as I believe it is fixed, please let me know if this is still a problem.