Skip to content

BUG: OAPI Codegen only generates the models that are present in an API YAML file. #1354

Closed as not planned

Description

@driif

After moving shared models into the 'Models' directory and generating APIs from folder paths, the shared models are not being utilized for generating Go structs. OAPI Codegen only generates the models that are present in an API YAML file. When a request or response utilizes a type from shared models, it generates this type in the following format:

type Response struct {
  Field *Struct {
    // Fields of shared models...
  }
}

However, it does not generate the actual model, which renders it impossible to use in the code.

Activity

  1. jamietanna commented on Nov 14, 2023

    @jamietanna
    Member

    Hey, would you mind providing more details? Preferably a minimal reproduction, with a specification, and the version of oapi-codegen you're using.

  2. driif commented on Nov 14, 2023

    @driif
    Author

    github.com/deepmap/oapi-codegen v1.12.3

    #!/bin/bash
    set -e
    
    readonly service="$1"
    readonly output_dir="$2"
    readonly package="$3"
    
    oapi-codegen -generate types -o "$output_dir/$service/openapi_types.gen.go" -package "$package" "api/paths/$service.yaml"
    oapi-codegen -generate server,spec -o "$output_dir/$service/openapi_api.gen.go" -package "$package" "api/paths/$service.yaml"

    I have folder
    api

    • models
      • shared models... (*.yaml)
    • paths
      • oapi-specifications... (*.yaml)

    By generating types, only types have been generated, that have been described in oapi-specifications.

    Example of models

    title: Address
    x-stoplight:
      id: yb09r4pj6l6yy
    type: object
    x-examples:
      Example 1:
        street: string
        streetNumber: string
        country: string
        zip: string
        city: string
        state: string
    properties:
      street:
        type: string
        x-faker: address.streetName
        x-stoplight:
          id: x0yx3rup05dsg
      streetNumber:
        type: string
        x-faker: datatype.number
        x-stoplight:
          id: 23e5ibszfi93q
      country:
        type: string
        x-faker: address.country
        x-stoplight:
          id: h5ksgulidcfzv
      zip:
        type: string
        x-faker: address.zipCode
        x-stoplight:
          id: nlok2ug5kr8su
      city:
        type: string
        x-faker: address.city
        x-stoplight:
          id: apvwo61n71f84
      state:
        type: string
        x-faker: address.state
        x-stoplight:
          id: a2pkvq10pl5db
    required:
      - street
      - streetNumber
      - country
      - zip
      - city
      - state

    type Address have not been generated. If i have in oapi-specification file reference to models, for example i have endpoint "Create User" and user has a field address, than user would be generated in format

    type User struct {
      Id uuid.UUID
      Name string
      Image *Image
      Adress *struct {
        Street string
        City string
        another fields...
        }
      }
      CreatedAt time.Time
    }
    
    But in this case I cannot use Address direct as a generated type. 
    
  3. jamietanna commented on Nov 17, 2023

    @jamietanna
    Member

    Can you please try and validate with v2? There have been quite a few changes since v1.12 that may have resolved this

  4. driif commented on Nov 22, 2023

    @driif
    Author

    Hi @jamietanna , I have tried it also with v2 Version, but unfortunately shared Models haven't been generated.

  5. markuswustenberg commented on Dec 6, 2023

    @markuswustenberg

    @driif is it maybe because your unused models have been pruned? Have you tried this setting in your config?

    output-options:
      skip-prune: true
  6. driif commented on Dec 6, 2023

    @driif
    Author

    @markuswustenberg Thank You for your reply, I have tried it, but unfortunately it also doesn't work in this case.

  7. jamesphillpotts-fr commented on Dec 19, 2023

    @jamesphillpotts-fr
    Contributor

    You can force the Address to be a full struct instead of an anonymous struct by adding x-go-type-name: Address to its yaml definition. However, I came across this issue while checking whether the subsequent issue I found after doing this had been raised or not. It only occurs with sub types of sub types though, so you should be ok.

  8. driif commented on Dec 19, 2023

    @driif
    Author

    @jamesphillpotts-fr Hi James, Thank You for your answer. In this particular case it works nice, but as you have mentioned - if there are any sub types of sub type, that they won't been generated. There is also one more thing - if sub type has a field with ENUM, than it won't be generated.

  9. jamesphillpotts-fr commented on Dec 20, 2023

    @jamesphillpotts-fr
    Contributor

    Yep, see #1397 :-)

  10. jamietanna commented on May 18, 2024

    @jamietanna
    Member

    @driif mind seeing if the changes suggested above by @jamesphillpotts-fr works for you, alongside the latest changes on master / from #1397 ?

  11. mromaszewicz commented on Feb 7, 2026

    @mromaszewicz
    Member

    The recommended approach for shared models across multiple spec files is to either: (1) consolidate schemas into a single spec, (2) use x-go-type-name extensions on referenced schemas (fixed for sub-types in #1397), or (3) use a separate models generation pass with the full spec. Since #1397 was merged, x-go-type-name now works for nested sub-types as well. Awaiting reply since May 2024 — please reopen if the issue persists with the latest version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions