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

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

Generated code has duplicate constructors for different enum variants #104

Open schoettl opened 4 months ago

schoettl commented 4 months ago

Hi there, thanks for this great code gen! I'd just like to hear some opinions how to best fix this issue:

In the Xentral ERP OpenAPI spec they use this inline declaration of a "Loose boolean type".

"/api/products": {
 "requestBody": {
   "content": {
     "schema": {
       "properties": {
                …
                  "allowPurchaseFromAllSuppliers": {
                    "description": "Loose boolean type which accepts boolean, number 0 or 1 and string 'true' or 'false'",
                    "example": "'true'",
                    "oneOf": [
                      {
                        "type": "boolean"
                      },
                      {
                        "type": "number",
                        "enum": [
                          0,
                          1
                        ]
                      },
                      {
                        "type": "string",
                        "enum": [
                          "true",
                          "false",
                          "True",
                          "False",
                          "TRUE",
                          "FALSE"
                        ]
                      }
                    ]
                  },

which translates to

data ExternalReference'createRequestBodyIsActive'OneOf3 =
   ExternalReference'createRequestBodyIsActive'OneOf3Other Data.Aeson.Types.Internal.Value -- ^ This case is used if the value encountered during decoding does not match any of the provided cases in the specification.
  | ExternalReference'createRequestBodyIsActive'OneOf3Typed Data.Text.Internal.Text -- ^ This constructor can be used to send values to the server which are not present in the specification yet.
  | ExternalReference'createRequestBodyIsActive'OneOf3EnumTrue -- ^ Represents the JSON value @"true"@
  | ExternalReference'createRequestBodyIsActive'OneOf3EnumFalse -- ^ Represents the JSON value @"false"@
  | ExternalReference'createRequestBodyIsActive'OneOf3EnumTrue -- ^ Represents the JSON value @"True"@
  | ExternalReference'createRequestBodyIsActive'OneOf3EnumFalse -- ^ Represents the JSON value @"False"@
  | ExternalReference'createRequestBodyIsActive'OneOf3EnumTRUE -- ^ Represents the JSON value @"TRUE"@
  | ExternalReference'createRequestBodyIsActive'OneOf3EnumFALSE -- ^ Represents the JSON value @"FALSE"@

where we have duplicate names in the type constructors.

I think it boils down to issue #85.

But in this case the enum is defined inline, not DRY in a scheme. So I'd need to patch dozens of generated files.

I didn't find an command line option to fix it.

As the request body is made by me and I will use Bool instead of some strings it should be save to delete the enum from the oneOf section. Maybe this can be done with jq. Or I just use sed but that might remove some occasions where it shouldn't.

Are there better ideas?

joel-bach commented 4 months ago

Hey @schoettl You are right, this is indeed the same problem as in the issue you referenced. There is currently no option to completely turn off the uppercasing of the first letter, so fixing the compilation itself is probably not possible without either:

So atm there is not really a good way to handle this I am afraid 😕

schoettl commented 4 months ago

Thanks @joel-bach for the pointer to --operation-to-generate, that's a good idea.