# Open-generator-cli problems, or Swagger/OpenApi recommendations

**URL:** <https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644>\
**Category:** Request Feedback\
**Created:** [November 9, 2019, 11:20am UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644 "2019-11-09T11:20:23Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![mattiasw](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mattiasw/32/908_2.png) [@mattiasw](https://discourse.elm-lang.org/u/mattiasw)\
**Post date:** [November 26, 2019, 8:23am UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/21 "2019-11-26T08:23:21Z")

</div>

> [@eriktimmers](#):
>
> elm make src/\*\*/\*.elm

Yes, that is the solution to my problem. I will run it each time I run open-generator.

---

<div class="post-metadata">

**Author:** ![lazurski](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lazurski/32/2785_2.png) [@lazurski](https://discourse.elm-lang.org/u/lazurski)\
**Post date:** [December 3, 2019, 7:53am UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/22 "2019-12-03T07:53:29Z")

</div>

Sorry for late response. I’m glad you like my ideas. Would you like me to re-base and open a PR with what I have so far? Or would you rather start from scratch?

> [@eriktimmers](#):
>
> Put all generated models in a single module ( `Api.Data` )

Very good idea. Even generation of requests will be easier that way.

---

<div class="post-metadata">

**Author:** ![eriktimmers](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eriktimmers/32/1114_2.png) [@eriktimmers](https://discourse.elm-lang.org/u/eriktimmers)\
**Post date:** [December 3, 2019, 9:03pm UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/23 "2019-12-03T21:03:10Z")

</div>

> [@lazurski](#):
>
> Sorry for late response. I’m glad you like my ideas. Would you like me to re-base and open a PR with what I have so far? Or would you rather start from scratch?

No problem. I already started working on a major refactoring. I’ve changed so much a PR wouldn’t make much sense.

### First release

As the matter of fact I planned on publishing a first version here on discourse today. It does not yet support `allOf` and recursive types, but it would be great to have some first feedback. The current changes include:

- Drop support for Elm 0.18;
- Drop `Main.elm`;
- Put all models in a single file;
- Nest generated code in a module called `Api`, i.e. `Api.Data` for models and `Api.Request.*` for operations;
- Introduce the `Request` type for improved customization of requests (operations).

You can download the jar [here](https://drive.google.com/open?id=1g_hzJwi7B8cKkIpMNcRA1x42sOXI7-ho). Use it by running

```auto
java -jar openapi-generator-cli.jar generate -i /your/openapi.yaml -g elm -o /path/to/write/to

# smoke test the output
cd /path/to/write/to
elm make src/**/*.elm

```

When you run into an issue and would like to report it, please provide a minimal **fully working** yaml/json file so I can easily reproduce the issue.

Any comments/suggestions/errors are welcome!

---

<div class="post-metadata">

**Author:** ![mattiasw](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mattiasw/32/908_2.png) [@mattiasw](https://discourse.elm-lang.org/u/mattiasw)\
**Post date:** [December 4, 2019, 11:00am UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/24 "2019-12-04T11:00:16Z")

</div>

Thank you! I tested it on my project. There are a generic object in the api, a class called ProblemDetails that belongs to [Asp.Net](http://Asp.Net) Core 3.0, so I need to delete the following from the swagger.json

```
     ,
      "extensions": {
        "type": "object",
        "nullable": true,
        "additionalProperties": {}
      }

```

Once I removed that and generate, it will create the non-ok recursive definition:

```
 type alias FormElement =
     { name : String
     :
     , elements : Maybe (List FormElement)
     }

```

After removing the field elements in the elm code and in the encoder+decoder, the Elm compiler complains about more things. I use Elm 0.19.

You can find the swagger.json file at [https://send.firefox.com/download/bcfa78a4c3bbf523/#Fq54jecJJPXS2jp6x1KyWA](https://send.firefox.com/download/bcfa78a4c3bbf523/#Fq54jecJJPXS2jp6x1KyWA)

---

<div class="post-metadata">

**Author:** ![eriktimmers](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eriktimmers/32/1114_2.png) [@eriktimmers](https://discourse.elm-lang.org/u/eriktimmers)\
**Post date:** [December 8, 2019, 9:40am UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/25 "2019-12-08T09:40:53Z")

</div>

Thanks for your report @mattiasw.

- I think it would only make sense to map the `additionalProperties: {}` to a `Dict String ()` as it can be of any type. However, this makes it quite useless;
- Recursion is still on its way;
- The other issues I have fixed.

Please try [this updated jar](https://drive.google.com/open?id=1lC7Odx92necU1ErfSzd3zVV_oDY6QYqx). Thanks.

---

<div class="post-metadata">

**Author:** ![mattiasw](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mattiasw/32/908_2.png) [@mattiasw](https://discourse.elm-lang.org/u/mattiasw)\
**Post date:** [December 9, 2019, 10:32am UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/26 "2019-12-09T10:32:15Z")

</div>

@eriktimmers Thank you for your hard work. Yes, you are correct. All problems are solved.

Regarding the extensions field for ProblemDetails, I think it should not even be there. There is a specification for it: [https://tools.ietf.org/html/rfc7807](https://tools.ietf.org/html/rfc7807) and I actually think that NSwag and/or Aspnet handles it incorrectly. I think the intention is that all key-value pairs in extensions should be lifted to top level in ProblemDetails.

Improvement suggestion: NSwag also includes the names for enums, and they could be used for creating union types. In my case, the names are String, Number and Bool. The union names should probably be something like` EValueType_String, EValueType_Number , EValueType_Boo`l. Here is a similar request: [https://github.com/swagger-api/swagger-ui/issues/5272](https://github.com/swagger-api/swagger-ui/issues/5272)

```
  "EValueType": {
    "type": "integer",
    "description": "",
    "x-enumNames": [
      "String",
      "Number",
      "Bool"
    ],
    "enum": [
      0,
      1,
      2
    ]
  },

```

Let me know when you fixed the recursion too, and I will remove all remnants of the old generated code.

---

<div class="post-metadata">

**Author:** ![eriktimmers](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eriktimmers/32/1114_2.png) [@eriktimmers](https://discourse.elm-lang.org/u/eriktimmers)\
**Post date:** [December 17, 2019, 8:35pm UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/27 "2019-12-17T20:35:44Z")

</div>

Thanks, @mattiasw. I wanted to refactor anyway so I have a good test case now.

I am not sure about the `x-enumNames` yet. They don’t seem part of the official spec and I’m not sure I want to support custom extensions at this point. Why is the enum not generated using the names in the first place?

I am almost done with the `allOf` support. After that I’ll start working on the recursion. My PR for that has just been merged into the generator.

---

<div class="post-metadata">

**Author:** ![mattiasw](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mattiasw/32/908_2.png) [@mattiasw](https://discourse.elm-lang.org/u/mattiasw)\
**Post date:** [December 18, 2019, 8:26am UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/28 "2019-12-18T08:26:41Z")

</div>

The x-enumNames feature seems only to be used by nswag currently, so I understand low prio.

Most likely, it is only needed for C#, which represents enums as numbers internally. It is only really useful in the intersection C# on the server, and a client language with enums or union types. So, it wouldn’t surprise me if the only openapi generator that supports it is the one for typescript, [https://www.typescriptlang.org/docs/handbook/enums.html](https://www.typescriptlang.org/docs/handbook/enums.html)

Keep us posted. To make it easier to find by other elm:ers, it is also listed at [https://korban.net/elm/catalog/](https://korban.net/elm/catalog/) but maybe you can add some elm specific information somewhere.

---

<div class="post-metadata">

**Author:** ![eriktimmers](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eriktimmers/32/1114_2.png) [@eriktimmers](https://discourse.elm-lang.org/u/eriktimmers)\
**Post date:** [December 18, 2019, 1:05pm UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/29 "2019-12-18T13:05:39Z")

</div>

> [@mattiasw](#):
>
> Most likely, it is only needed for C#, which represents enums as numbers internally.

Even if C# uses numbers internally the (de)serialization could handle strings, right? But OK, I do not know the details for that.

Here’s [a new release](https://drive.google.com/open?id=1DCqUuI5RoCDWHgOsf01DFbBDF7CB3vfi) including support for `allOf`. Using `allOf` adds all properties to another type. If the original type also has a `discriminator`(1) the original type is added using composition and (2) a custom type is creating with all the possible sub-types and a fallback base type (see my edited comment above).

---

<div class="post-metadata">

**Author:** ![mattiasw](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mattiasw/32/908_2.png) [@mattiasw](https://discourse.elm-lang.org/u/mattiasw)\
**Post date:** [December 18, 2019, 3:12pm UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/30 "2019-12-18T15:12:21Z")

</div>

The indentation of the generated files stopped working, so you generate something like this:

```
type alias BOAttachment =
    { filename : String
, url : String
}

```

Used to work fine.

---

<div class="post-metadata">

**Author:** ![eriktimmers](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eriktimmers/32/1114_2.png) [@eriktimmers](https://discourse.elm-lang.org/u/eriktimmers)\
**Post date:** [December 18, 2019, 7:05pm UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/31 "2019-12-18T19:05:59Z")

</div>

That’s weird. “Works on my machine”. Can you try on another device?

---

<div class="post-metadata">

**Author:** ![eriktimmers](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eriktimmers/32/1114_2.png) [@eriktimmers](https://discourse.elm-lang.org/u/eriktimmers)\
**Post date:** [December 18, 2019, 8:35pm UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/32 "2019-12-18T20:35:21Z")

</div>

Here’s [a new one](https://drive.google.com/open?id=1exCagZY5osnsGfNBRWRB_2Y0IZKrc9Yz) with support for recursive types! Please give it a try!

---

<div class="post-metadata">

**Author:** ![mattiasw](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mattiasw/32/908_2.png) [@mattiasw](https://discourse.elm-lang.org/u/mattiasw)\
**Post date:** [December 18, 2019, 9:15pm UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/33 "2019-12-18T21:15:39Z")

</div>

I still have the indentation problem. I am using the swagger.json file I linked to previously. After adding elm-format, it compiles fine. Remove it, and Data.elm fails.

The recursive structure seems ok and compiles, however, I have not tested it yet in my code.

This is the Windows cmd file I run

```
java -jar openapi-generator-cli-4.jar generate -g elm -i swagger.json -o src/Swagger/
cd src/Swagger/
call elm-format --yes ./src/
call elm make src/Api.elm src/Api/Data.elm src/Api/Request/Acc.elm src/Api/Request/Account.elm src/Api/Request/Attachment.elm src/Api/Request/Cmd.elm src/Api/Request/CmdAdmin.elm src/Api/Request/CmdGuest.elm src/Api/Request/Frm.elm src/Api/Request/Grid.elm src/Api/Request/Home.elm src/Api/Request/Ins.elm src/Api/Request/InsPdf.elm src/Api/Request/InsView.elm src/Api/Request/LoggedOut.elm src/Api/Request/NoAccount.elm src/Api/Request/SignIn.elm src/Api/Request/UserContext.elm src/Api/Request/Usr.elm src/Api/Request/Version.elm
cd ..
cd ..
```

---

<div class="post-metadata">

**Author:** ![eriktimmers](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eriktimmers/32/1114_2.png) [@eriktimmers](https://discourse.elm-lang.org/u/eriktimmers)\
**Post date:** [December 19, 2019, 7:08am UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/34 "2019-12-19T07:08:08Z")

</div>

I’ll see if I can reproduce it on another machine.

Forgot to export the recursion type. Try [this one](https://drive.google.com/open?id=1609AttwpoJIUZ4rzrfMJiNuQypaVsdAj) instead.

---

<div class="post-metadata">

**Author:** ![eriktimmers](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eriktimmers/32/1114_2.png) [@eriktimmers](https://discourse.elm-lang.org/u/eriktimmers)\
**Post date:** [December 21, 2019, 8:08am UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/35 "2019-12-21T08:08:57Z")

</div>

Another [update](https://drive.google.com/open?id=1-Wb6lAXERziCcE3sGv1ig9A2RcmPDDpW)!

- Add missing catch-all type for `allOf`s;
- Drop support for post-processing of files;
- Only import UUID/Time when actually used.

---

<div class="post-metadata">

**Author:** ![mattiasw](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mattiasw/32/908_2.png) [@mattiasw](https://discourse.elm-lang.org/u/mattiasw)\
**Post date:** [December 22, 2019, 11:00pm UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/36 "2019-12-22T23:00:13Z")

</div>

Applied the code generated with openapi-generator-cli-6.jar to the whole project. Two bugs detected, first one in this entry.

The type is enum over ints, however the code assumes the server sends values as string:

```
  "EValueType": {
    "type": "integer",
    "description": "",
    "x-enumNames": [
      "String",
      "Number",
      "Bool"
    ],
    "enum": [
      0,
      1,
      2
    ]
  },

```

I had to patch the generated code like this:

```
eValueTypeDecoder : Json.Decode.Decoder EValueType
eValueTypeDecoder =
    Json.Decode.string
        |> Json.Decode.andThen
            (\str ->
                case str of
                    "0" ->
                        Json.Decode.succeed EValueType0

                    "1" ->
                        Json.Decode.succeed EValueType1

                    "2" ->
                        Json.Decode.succeed EValueType2

                    other ->
                        Json.Decode.fail <| "Unknown type: " ++ other
            )

```

to

```
eValueTypeDecoder : Json.Decode.Decoder EValueType
eValueTypeDecoder =
    Json.Decode.int
        |> Json.Decode.andThen
            (\str ->
                case str of
                    0 ->
                        Json.Decode.succeed EValueType0

                    1 ->
                        Json.Decode.succeed EValueType1

                    2 ->
                        Json.Decode.succeed EValueType2

                    other ->
                        Json.Decode.fail <| "Unknown type: " -- ++ other
            )

```

 ![didnt-get-error](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/2X/8/8c13edbd2ab3d5e2aa772c12084081a845bbc665.png)

Also, I expected the Error case to pop up just like during network problem, but instead I had to debug the javascript to find out why it failed.

---

<div class="post-metadata">

**Author:** ![mattiasw](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mattiasw/32/908_2.png) [@mattiasw](https://discourse.elm-lang.org/u/mattiasw)\
**Post date:** [December 22, 2019, 11:02pm UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/37 "2019-12-22T23:02:28Z")

</div>

The urls contains a double // in url, for example [http://localhost:5000//Acc/current/forms](http://localhost:5000//Acc/current/forms)

Fixed by removing the initial / in the path on all files in Request directory.

```
cmdGet : String -> Api.Request Api.Data.Form
cmdGet cmdName =
    Api.request
        "GET"
        "/Cmd/{cmdName}"
        [( "cmdName", identity cmdName )]
        []
        []
        Nothing
        Api.Data.formDecoder

```

to

```
cmdGet : String -> Api.Request Api.Data.Form
cmdGet cmdName =
    Api.request
        "GET"
        "Cmd/{cmdName}"
        [( "cmdName", identity cmdName )]
        []
        []
        Nothing
```

---

<div class="post-metadata">

**Author:** ![mattiasw](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mattiasw/32/908_2.png) [@mattiasw](https://discourse.elm-lang.org/u/mattiasw)\
**Post date:** [December 22, 2019, 11:10pm UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/38 "2019-12-22T23:10:41Z")

</div>

The indentation problem is still there, but I run elm-format every time, so not important.

Also, any special reason you have the parameters for Api.send in that order? If you swap them, same order as v1 Http.send, all I have to do is replace my Http.send to Api.send in the rest of the project.

---

<div class="post-metadata">

**Author:** ![eriktimmers](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/eriktimmers/32/1114_2.png) [@eriktimmers](https://discourse.elm-lang.org/u/eriktimmers)\
**Post date:** [December 23, 2019, 12:52pm UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/39 "2019-12-23T12:52:08Z")

</div>

@mattiasw thanks for the feedback!

> [@mattiasw](#):
>
> The type is enum over ints, however the code assumes the server sends values as string

Good point. I’ll make it work with integers as well.

> [@mattiasw](#):
>
> Also, I expected the Error case to pop up just like during network problem, but instead I had to debug the javascript to find out why it failed.

Can you elaborate on this? I don’t understand what you mean.

> [@mattiasw](#):
>
> The urls contains a double // in url

I think you should fix the base path in your OpenAPI JSON. It should not end with `/` if you want to get rid of the `//`. The path in the request itself is correct.

> [@mattiasw](#):
>
> The indentation problem is still there, but I run elm-format every time, so not important.

I still would like to have this fixed. Were you able to run this on a second machine? I still haven’t been able to reproduce this.

> [@mattiasw](#):
>
> Also, any special reason you have the parameters for Api.send in that order?

Nope I did that by accident. Already fixed it in my unreleased code.

I’ve also started working on a Gitbook for some background, guidance and technical details on the OpenAPI Generator and Elm. Would be great if you could have a look when it’s ready.

---

<div class="post-metadata">

**Author:** ![mattiasw](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/mattiasw/32/908_2.png) [@mattiasw](https://discourse.elm-lang.org/u/mattiasw)\
**Post date:** [December 23, 2019, 1:20pm UTC](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644/40 "2019-12-23T13:20:45Z")

</div>

Thanks @eriktimmers for the quick response!

> [@eriktimmers](#):
>
> Can you elaborate on this? I don’t understand what you mean.

Look at the screenshot above, that is how I found that Elm expected a string, and got an int. I expected it would have been by an Err-msg, i.e. that my client would have presented it as any Http-ajax-error message, for example a network timeout.

> [@](#):
>
> I think you should fix the base path in your OpenAPI JSON. It should not end with `/` if you want to get rid of the `//` . The path in the request itself is correct.

In the swagger.json file, there is no /, it says:

```
  "servers": [
    {
      "url": "http://localhost:5000"
    }
  ],

```

[Previous page](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644.md?page=1)

[Next page](https://discourse.elm-lang.org/t/open-generator-cli-problems-or-swagger-openapi-recommendations/4644.md?page=3)
