Skip to content

Cannot create a useable schema #288

@schmurfy

Description

@schmurfy

I am taking the reverse road most usually take by generating openapi3 documentation from a minimal http framework on top of chi, the start was pretty fast and I can generate everything I need for the request path arguments but I am stuck trying to generate a schema for the requestBody of an operation, my best attempt so far is:

type Pet struct {
  Id   string `json:"id"`
  Name string `json:"name"`
}

gen := openapi3gen.NewGenerator()

// bodyField points to `Pet` reflected type
bodySchema, err := gen.GenerateSchemaRef(bodyField.Type)
// error is checked but I removed it for clarity

body := openapi3.NewRequestBody()
body.Content = openapi3.NewContentWithJSONSchema(bodySchema.Value)
bodyRef := &openapi3.RequestBodyRef{
  Value: body,
}

op.RequestBody = bodyRef
swagger.AddOperation(pattern, method, op)

Aside from being really verbose (there may be faster way to do things but that's the best I found for now) this works but generates:

{
  "content": {
    "application/json": {
      "schema": {
        "properties": {
          "id": {
            "$ref": "string"
          },
          "name": {
            "$ref": "string"
          }
        },
        "type": "object"
      }
    }
  }
}

this cause the viewers to try and request <my-domain>/string and i am not sure what I missed, from what I understand internal references should start with "#" but I see no way of influencing what ref are generated and I feel my lack of familiarity with openapi3 does not help xD

Any help or pointer in the right direction will be greatly appreciated :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions