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

[CLI] Generating an empty object using swagger:type object does not work as expected. #2758

Closed
adam-debkowski opened this issue Apr 12, 2022 · 1 comment

Comments

@adam-debkowski
Copy link

Problem statement

I'm trying to generate an OpenAPI spec from code that skips the additionalProperties when generating spec based on a map[string]interface{} type.
So I want a definition of:

type CustomMetadata map[string]interface{}

To generate:

  CustomMetadata:
    type: object

As far as I observed, the additionalProperties property comes from using a map[string]interface{}, since when using a simple interface{}, it generates the expected output. A definition:

type CustomMetadata interface{}

Generates:

  CustomMetadata:
    type: object

I have tried to achieve that using swagger:format. I expected a definition of:

// swagger:type object
type CustomMetadata map[string]interface{}

To generate:

  CustomMetadata:
    type: object

so without the additionalProperties field. But it is not the case and the // swagger:type object seems to be ignored. The same happens with a type definition of type CustomMetadata interface{}: no matter the swagger:type, the type definitions based on a map or interface always generate their respective OpenAPI specs disregarding a swagger:type.

I've also checked other types, and so for example:

// swagger:type object
type CustomMetadata string

Generates:

  CustomMetadata: {}

This is still not ideal, since as far as I know, the {} in Swagger 2.0 means "any type", and not an object specifically. But it shows that the swagger:type is taken into account in this case.

The current solution is to use interface{} instead of map[string]interface{} which generates expected output, but is not ideal since it requires a lot of changes in the actual application code.

Swagger specification

definitions:
  CustomMetadata:
    additionalProperties:
      type: object
    type: object
info:
  description: Test description
  title: Test
  version: 1.2.3
paths:
  /pets:
    get:
      operationId: listPets
      responses:
        "200":
          $ref: '#/responses/someResponse'
responses:
  someResponse:
    description: ""
    schema:
      properties:
        CustomMetadata:
          $ref: '#/definitions/CustomMetadata'
      type: object
swagger: "2.0"

Steps to reproduce

// Test
//
// Test description
//
//     Version: 1.2.3
//
// swagger:meta
package main

// swagger:route GET /pets listPets
//
//     Responses:
//       200: someResponse

// swagger:response someResponse
type SomeResponse struct {
	// in: body
	Body struct {
		CustomMetadata CustomMetadata
	}
}

// swagger:type object
type CustomMetadata map[string]interface{}

Run:

swagger generate spec -o api.yml

Environment

swagger version:

version: v0.29.0
commit: 09ae1192ca9a941bbb534aca09e6bdc562c95ef3

go version: go version go1.17.2 linux/amd64
OS: Ubuntu 18.04.6 LTS

@adam-debkowski
Copy link
Author

It seems that the #2776 fixes this issue so I'm closing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant