Skip to content

use of AnyOf generates struct type names starting with a number #1121

Closed

Description

@d4anvdh

Trying to compile my schema using this command:
oapi-codegen -package generated openapi/api.yaml > api.gen.go
I'm on version v1.13.0
openapi: 3.0.0

This error is given:

error generating code: error formatting Go code
: generated.go:263:16: '_' must separate successive digits (and 1 more errors)

I've tried to create a minimal example where this issue occurs:

This is a simple schema with cats, dogs and rats.
api.yaml.txt

This is what's written to stderr:
api.gen.go.txt

You can see the error at the top and bottom of the go file.
At line 263 and 320, invalid Go is generated.

This works up until version 1.12.1, but after that, it is broken.

Activity

  1. daleef-rahman commented on Jul 3, 2023

    @daleef-rahman
    Contributor

    @Krulknul The issue seems to be in the client code only. Looks like client is generated by default option in v1.13.0. You can try skipping the client code generation by using the command
    oapi-codegen -package generated -generate types,server,spec api.was.yaml > api.gen.new2.go

    Alternatively, if you want to generate the client as well, you can do a small refactoring in the spec file. That should also work.
    api.was.yaml.txt

    I will try to investigate and fix the root cause as my first contribution to this repo 🙂

  2. jvgutierrez commented on Apr 2, 2024

    @jvgutierrez

    I'm hitting this issue trying to generate the client code for USDA Food Central API (https://app.swaggerhub.com/apis/fdcnal/food-data_central_api/1.0.1). Is feasible to work around this issue without modifying the spec?

  3. alejo-rivera commented on Jul 19, 2024

    @alejo-rivera

    I'm hitting this issue trying to generate the client code for USDA Food Central API (https://app.swaggerhub.com/apis/fdcnal/food-data_central_api/1.0.1). Is feasible to work around this issue without modifying the spec?

    Found myself here for the same reason. I cloned his fork from #1178 and got a workable client. I look forward to see this finished and merged though!

  4. vasyl-haievyi commented on Sep 11, 2024

    @vasyl-haievyi

    Thank you @alejo-rivera for the suggestion. I also was able to generate a valid client while using the fix. I have faced this issue while working with Hapio APIs - https://docs.hapio.io/hapio-api-definition/Hapio-API.v1.yaml

    Generated code was:

    ...
    var dest struct {
        Data *[]200_Data_Item`json:"data,omitempty"`
    ...
    : hapio.go:8854:16: '_' must separate successive digits (and 1 more errors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions