oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

`types` generation not generating response body types

Open danthegoodman1 opened this issue 4 years ago • 1 comments

The imported response bodies are not getting generated as go types with the following file set. As you can see the matchmakeResponse.yml type is not generated.

I have the following set of files:

openapi.yml

openapi: '3.1.0'
info:
  version: 1.0.0
  title: Matchmaking
servers:
  - url: ':8080'
    description: localhost
paths:
  /hc:
    $ref: paths/hc.yml

  /lobbyPrices:
    $ref: paths/lobbyPrices.yml

  /matchmake:
    $ref: paths/matchmake.yml

components:
  securitySchemes:
    Bearer:
      $ref: ./bearerAuth.yml

paths/matchmake.yml

post:
  security:
    - Bearer: []
  tags:
    - Matchmaking
  summary: Find a match for a given game
  requestBody:
    content:
      application/json:
        schema:
          $ref: ../schemas/matchmakeRequest.yml
  responses:
    '200':
      description: Match found
      content:
        application/json:
          schema:
            $ref: '../schemas/matchmakeResponse.yml'

schemas/matchmakeRequest.yml

type: object
required:
  - game
properties:
  game:
    type: string
    enum:
      - CYBERWORMS
    x-oapi-codegen-extra-tags:
      validate: required
  lobbyPrice:
    type: number
    description: The lobby price in cents.

schemas/matchmakeResponse.yml

type: object
required:
  - token
  - address
properties:
  token:
    type: string
    description: The Bearer JWT token that must be passed to the game server as the
  address:
    type: string
    description: The address of the game server to connect to

This is the configuration file:

output: httpServer/http.types.gen.go
package: httpServer
generate:
  - types

And this is the golang types file generated:

// Package httpServer provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/deepmap/oapi-codegen version v1.9.1 DO NOT EDIT.
package httpServer

const (
	BearerScopes = "Bearer.Scopes"
)

// GetLobbyPricesParams defines parameters for GetLobbyPrices.
type GetLobbyPricesParams struct {
	// The game to get lobby prices for, will find in the region the backend deems the most relevant.
	Game GetLobbyPricesParamsGame `json:"game"`
}

// GetLobbyPricesParamsGame defines parameters for GetLobbyPrices.
type GetLobbyPricesParamsGame string

// PostMatchmakeJSONBody defines parameters for PostMatchmake.
type PostMatchmakeJSONBody struct {
	Game PostMatchmakeJSONBodyGame `json:"game" validate:"required"`

	// The lobby price in cents.
	LobbyPrice *float32 `json:"lobbyPrice,omitempty"`
}

// PostMatchmakeJSONBodyGame defines parameters for PostMatchmake.
type PostMatchmakeJSONBodyGame string

// PostMatchmakeJSONRequestBody defines body for PostMatchmake for application/json ContentType.
type PostMatchmakeJSONRequestBody PostMatchmakeJSONBody

Generated like: oapi-codegen -config httpServer/types.config.yml ./openapi/openapi.yml

I have found that if I add the reponse body as a component/schema/matchmakeResponse and reference it as #components/schemas/matchmakeResponse I can get it, but I have to define the whole response in that main file as well, the $ref import does not get respected.

I have also tried with an inline response schema (no $ref and it too does NOT include the type)

danthegoodman1 avatar Jan 28 '22 14:01 danthegoodman1

Is that ever getting fixed ? just had the same problem hindering me to generate a Schema with bodies ..

78bits avatar Nov 10 '25 12:11 78bits