map - support for other key types besides string
The current swagger 2.0 implementation of map defaults to a map[string] with the value defined in additionalProperties. In most language implementations, the map key can contain anything that is comparable (using == to compare). For primitives, this usually includes bool, numeric, and string.
Proposal: enhance swagger specification to allow spec writer to define the type of key in the map.
Possible Approach - modify Schema Object, allow new "type" of "map". Add new property for mapType: enum [ valid map key types ], and define the value in the items element - just like arrays. Remove additionalProperties from Schema Object.
We're trying to follow the JSON Schema definitions, so it's more likely that patternedProperties would be used as the solution. However, let's open it for discussion and get some community feedback.
Understood. I'm going to attempt to talk through schema validation and where using keywords of json schema as properties in a json schema don't actually execute the validation semantics, but execute the semantics of the defined property. Just to let you know, I'm probably going to do a poor job of this.
The best example in the swagger schema is the "required" property. It is used on parameters and is a boolean. At first glance, this appears to violate the json schema spec in that required must be a string array. But it does not because you are actually just using a keyword name as a variable name and then defining different validation semantics on that variable. Instead of inheriting from the parent schema, you are overriding it.
For the most part, you could say the swagger spec uses inheritance in that if it has a variable name that is a keyword in the json schema, it refers to the json schema for the validation. That is evident just looking at the swagger schema definition for "schema" which basically inherits all of the validation from the json-schema schema
Where I am going with this is in an inheritance model, the class can choose to extend the functionality of the parent. For example, the swagger "schema" definition could have a property (variable) named "type" that has validation semantics of:
oneOf: [
{
"$ref": "http://json-schema.org/draft-04/schema#/properties/type" // inheritance
},
{
enum: ["map"] // extension
}
]
And then there would be a allOf validation for items and mapKeyType.
So you are following the JSON Schema definitions, just applying inheritance principles that allow you to extend the model. Hope this makes sense, but understand if it doesn't.
Right, the difference is that we're talking about the Schema Object which is an extended subset of JSON Schema. The rest of the Swagger specification does not follow the JSON Schema standard (as you mentioned with the required property) as that's an entirely proprietary structure.
For extensions to the Schema Object though, we'd probably prefer using the existing solutions of JSON Schema if those exist, even though JSON Schema indeed allows you to extend it with your own structures.
:pray:
@rmullinnix Is this sufficiently addressed in the OpenAPI Spec v3?
https://github.com/OAI/OpenAPI-Specification/releases
@RobDolinMS I think this issue is preventing the usage of swagger-api/swagger-core#2356 according to @webron, so I don't think OAI Spec v3 fixed this.
Is there some behind the scenes work done on this? I think using Maps with Enums as keys is a valid use case, which should be possible.
Does modern JSON Schema's propertyNames keyword help with this?
Taking a look at the referenced swagger issue about map keys being within an enum, I am declaring that, yes, propertyNames fixes this, and with the full adoption of JSON Schema in OAS 3.1, propertyNames will be available in that release. So I'm closing this as fixed.