Haskell-OpenAPI-Client-Code-Generator icon indicating copy to clipboard operation
Haskell-OpenAPI-Client-Code-Generator copied to clipboard

Generated code has duplicate constructors for different enum variants

Open schoettl opened this issue 1 year ago • 2 comments

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?

schoettl avatar Apr 21 '24 10:04 schoettl

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:

  • Changing the specification to remove the source of the duplication
  • Excluding this operation from the generation by only specifying the operations you use (which compile) using the operation-to-generate flag (can be applied multiple times to generate all the operations necessary).

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

joel-bach avatar Apr 24 '24 06:04 joel-bach

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

schoettl avatar Apr 24 '24 07:04 schoettl