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

Bug:generate types code

Open tanberBro opened this issue 2 years ago • 1 comments

Bug description

Generated types code, containing useless models.

Reproduction

openapi.yaml

openapi: 3.0.3
info:
  title: Swagger authorization - OpenAPI 3.0
  description: |-
    authorization api doc
  version: v1
servers:
  - url: http://localhost:8080/v1

tags:
  - name: health
    description: health check
paths:
  /health/liveness:
    get:
      tags:
        - health
      summary: liveness check
      description: liveness check
      responses:
        '200':
          description: Successful liveness check
  /health/readiness:
    get:
      tags:
        - health
      summary: readiness check
      description: readiness check
      responses:
        '200':
          description: Successful readiness check
components:
  schemas:
    ResourceTree:
      type: object
      properties:
        code:
          type: string
          example: application
        scope:
          type: string
          example: namespace
        name:
          type: string
          example: 应用
        children:
          type: array
          items:
            $ref: '#/components/schemas/ResourceTree'
        createTime:
          type: string
          example: 2006-01-02 15:04:05.000
        updateTime:
          type: string
          example: 2006-01-02 15:04:05.000
      required:
        - code
        - scope
        - name
        - children
        - createTime
        - updateTime

execute command

oapi-codegen -package health -generate types -include-tags health openapi.yaml > types.gen.go

types.gen.go

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

// ResourceTree defines model for ResourceTree.
type ResourceTree struct {
	Children   []ResourceTree `json:"children"`
	Code       string         `json:"code"`
	CreateTime string         `json:"createTime"`
	Name       string         `json:"name"`
	Scope      string         `json:"scope"`
	UpdateTime string         `json:"updateTime"`
}

Expected behavior

The above should not contain the ResourceTree structure in the generated types.gen.go file, it should only appear in the interface types file that references it.

tanberBro avatar Jan 23 '24 06:01 tanberBro

Hi, are you expecting that the usage of include-tags would make sure that only types that are referenced by paths with the tag health get included? (If so, I think that makes sense, and would be something for us to improve on)

jamietanna avatar Jan 23 '24 09:01 jamietanna