hapi-swagger / hapi-swagger

A Swagger interface for hapi
https://hapi.dev/
MIT License
913 stars 420 forks source link

Ordered Array has string type #290

Closed Calavoow closed 8 years ago

Calavoow commented 8 years ago

When creating a schema for data returned from the API as follows:

const readObjectSchema = Joi.object().keys({
    'userxarticle' : Joi.array().ordered(
            Joi.number().description('userid'),
            Joi.number().description('articleid')
    ).description('User and Article tuple')
});

An example instance could be

{'userxarticle': [1,2]}

The output of the model type on the swagger doc website is as follows

{
  "userxarticle": [
    "string"
  ]
}

when adding the schema to the hapi-swagger plugin options as

plugins: {
    'hapi-swagger':{
        responses: {
            '200': {
                description:"The article was already present in the db for this user.",
                schema: readObjectSchema
            }, ...
        }
    }
}

Toggling required as in #249 has no effect.

Did I make an error here in specifying the schema, or attaching it to the hapi-swagger plugin? Or is this a bug in converting the type of a tuple?

glennjones commented 8 years ago

Sorry its taken me so long to answer this issue.

The current code defaults to a string if the type cannot be found or understood in term of OpenAPI/Swagger spec .

I have had a look at the OpenAPI/Swagger spec and its does not look like it supports typing individual items in an array. Cannot not see any work around for this so I am going to add array.ordered(type) as an unsupported feature in the readme.

I don't think that this type of feature will be add to OpenAPI/Swagger spec soon. They have to deal with how it would effect the code generation tools for strongly typed languages first.

I will close the issues, but if you wish to discuss more it please feel free to comment

Calavoow commented 8 years ago

Thanks. In strongly typed languages this is simply a tuple or HList, so hopefully it can be added some time.