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

Array of schema inside another schema #17

Closed
rhazegh opened this issue Apr 29, 2015 · 7 comments
Closed

Array of schema inside another schema #17

rhazegh opened this issue Apr 29, 2015 · 7 comments

Comments

@rhazegh
Copy link

rhazegh commented Apr 29, 2015

I want to define a swagger schema/model for Book that has and array of chapters inside. I want the following JSON:

{
  "name": "string",
  "num_pages": 0,
  "chapters": [
    {
      "title": "string",
      "text": "string"
    }
  ]
}

So I defined the following two swagger schemas:

  swagger_schema :Chapter do
    property :title do
      key :type, :string
    end
    property :text do
      key :type, :string
    end
  end
  swagger_schema :Book do
    property :name do
      key :type, :string
    end
    property :num_pages do
      key :type, :integer
      key :format, :int64
    end
    property :chapters do
      key :'$ref', :Chapter
      key :type, :array
    end
  end

But I end up getting the following in the generated swagger documentation for Book:

{
  "name": "string",
  "num_pages": 0,
  "chapters": 
    {
      "title": "string",
      "text": "string"
    }
}

What is the proper way to define an array of a schema inside another schema?

@fotinakis
Copy link
Owner

The keys are case-sensitve, can you try :Chapter instead of :chapter?

@rhazegh
Copy link
Author

rhazegh commented Apr 29, 2015

My bad. I corrected the typo I made above. I just fabricated the above example to describe the problem I am having. My real models have so many properties and the capitalization is correct on the actual models.

@fotinakis
Copy link
Owner

Also, in Swagger JSON I believe you must use an items definition if you use the array type anywhere.

@rhazegh
Copy link
Author

rhazegh commented Apr 29, 2015

yes, I tried to use items later, but what is the swagger-blocks syntax for it?

@fotinakis
Copy link
Owner

I just added an example for arrays in schema objects. So you would do something like:

    property :chapters do
      key :type, :array
      items do
        key :'$ref', :Chapter
      end
    end

Let me know if that works.

@rhazegh
Copy link
Author

rhazegh commented Apr 29, 2015

Yes it works 👍 Thank you 😃

@ayadavkrt
Copy link

ayadavkrt commented Jan 24, 2019

property :something do
      key :type, :array
      items do
        key :'$ref', :Chapter
        key :'$ref', :Disaster
        key :'$ref', :Creator
      end
    end

How can one do this ???

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

3 participants