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

Incorrect go code generated #23

Open
ISim opened this issue May 6, 2021 · 7 comments
Open

Incorrect go code generated #23

ISim opened this issue May 6, 2021 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@ISim
Copy link

ISim commented May 6, 2021

Hello,

I read about this project in go subredit and I like the idea of go generator from swagger.
I tried to check it on a little bit complicated API (https://openapi3.ocs.io/browse/index.html) I have to deal with (I'm not an author of API, just consumer).

go run . -swagger-addr https://openapi3.ocs.io/ocs-io-openapi3.yaml -package example -path ./example

It fails with:

2021/05/06 09:26:14 failed rending into file '/Users/simoniki/tmp/go-oas3/example/routes_gen.go': Error 73:10: expected '(', found '-' (and 10 more errors) while formatting source:
// This file is generated by github.com/mikekonan/go-oas3. DO NOT EDIT.

package example

import (
"context"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
chi "github.com/go-chi/chi"
cast "github.com/spf13/cast"
"net/http"
)

[snip]

The output is not quite clear (because it failed during formatting operation, but the generated code printed on stdout is suspicious. For example:

 func RM - DocumentsHandler (impl RM - DocumentsService,r chi.Router,hooks * Hooks) (http.Handler) {
router := & rm - documentsRouter {router : r,service : impl,hooks : hooks}

 router . mount ()

 return  router . router
}

For the first sight it looks like using the first item from "tags" as the name of function without sanitizing. Is it good idea to use tags for names?

And maybe one more question: I usually use generators only for DTOs. The openapi-tool generator has the option to generate only model, without router. I think it should be fine to have the same option here as well. Sometimes go-chi is not desired router or other reason.

@mikekonan
Copy link
Owner

Thanks for your issue, I just quickly reviewed your questions.

Q: I usually use generators only for DTOs
A: Yes, actually I planned to add a possibility to specify what scope needs to be generated:)

Q: Is it a good idea to use tags for names?
A: That is a kind of core idea for generating chi-router stubs segregating the interfaces. Also, I fully agreed that the generator should not panic when the tag is not set, and I need to think about how I need to handle such a case.

Regarding the code-generation issue, I need some time to review it.

@mikekonan mikekonan self-assigned this May 7, 2021
@mikekonan mikekonan added the bug Something isn't working label May 7, 2021
@mikekonan
Copy link
Owner

Just looked deeper and found some issues, will look into it.

@mikekonan
Copy link
Owner

Tags issue fixed in v1.31, also there are some issues with anonymous slices and maps.

Also, found that you have a 5XX status code, so it won't work anyway.

@ISim
Copy link
Author

ISim commented May 24, 2021

Thank you for your effort I will try it. I also found another swagger that panicked your code so I will try it with the new version.

Yes, you are right the "5XX" is not correct - the API is not mine and I wrote email about this a week ago. Also about properties without types.

@ISim
Copy link
Author

ISim commented May 28, 2021

Hello again,
I have tried building client with a new version (64d1894) and ended with similar issues. For example:

 type GenericInvoiceLayout 0

or even empty type declaration for DepositLayout

 type CreateRecurrentFeeSubscription CreateSubscription

 type DepositLayout

 type documentLine struct{
GlCode * interface{} `json:"glCode"`
ItemPrice * int `json:"itemPrice"`
ProductCode * string `json:"productCode"`
[....snip...]

I uploaded the swagger to swaggerhub to check these types and it looks OK. However, I used swagger-codegen-cli-v3 and it generated this:

type DepositLayout struct {
	CustomAttributes *map[string]interface{} `json:"customAttributes,omitempty"`
}

So, the swagger-codegen-cli-v3 used only last item from "allOf construction, instead merge two object definitions together.

    DepositLayout:
      allOf:
        - type: object
          description: 'Document header layout, from this other layouts are inherited.'
          properties:
            refId:
              description: Ref ID of the Document.
              type: string
              minLength: 1
              maxLength: 40
[...shortened for readability...]
        - type: object
          description: Deposit Document Layout.
          properties:
            customAttributes:
              description: 'Custom attributes, Key - Value (String) Map'
              $ref: '#/components/schemas/CustomAttributes'

I uploaded swagger to swaggerhub.com and DepositLayout is shown complete with all properties. But when I clicked to download go client, the structure DepositLayout is the same as from swagger-codegen-cli-v3.

So, the schema of OCS is a tough nut to crack....

Currently I have to give up my usual approach (generate model only objects from swagger to extra package to keep them sync with swagger) and I generate model using swagger-codegen-cli-v3 to extra directory in the project and manually copying selected part to my model.

I would prefer to generate model via go-oas3, because I run swagger-codegen-cli-v3 using docker to keep my environment java free, but there is no easy way to format go file with syntax errors produced by go-oas3.

@mikekonan
Copy link
Owner

mikekonan commented Jul 9, 2021

Hello, sorry for the delayed response (I've missed notification somehow).
Could you please share your swagger file? I mean the part that includes $ref

@mikekonan
Copy link
Owner

Actually, "allOf" feature supported - https://github.com/mikekonan/go-oas3/blob/main/generator/type.go#L60, but I'm now sure it works properly with objects, need to check it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants