Requirements get lost when model is made a definition
I have a simple case where I want to reuse a definition for a time period:
val period = $(text().constraint(pattern("^P.*"))).refName("Period").$$
This gets translated in the following definition:
"Period": {
"type": "string"
}
Also, if I add the required constraint to the field when used in a mapping like so:
field("tenor", period.constraint(required()))
This required field is not reflected in the spec.
You reported two problems here.
For 1, pattern and required are a property's properties. For this case, it's a primitive model, and there isn't a property member in current swagger-core implementation, so no where to place the pattern and required.
For 2, you made a mistake. required is already added in its parent's required list, like this:
"xxxParentModel": {
"type": "object",
"required": [..., "tenor"],
"properties": {...}
}
Tips: if you omit the ref name from period definition, it'll work well.
To reuse a mapping definition, you needn't give it a ref name.
Ok, maybe I'm completely misunderstanding, but if I manually create a primitive definition like I had, and check it in the swagger editor, it is valid, e.g.
"Period": {
"type": "string",
"pattern": "^P.*"
}
I know I can reuse the definitions in code, but I want to avoid that the json output is cluttered with repeated pattern definitions, hence I want to be able to push it down into the definitions.
According to the spec, the Definitions Object exists of Schema Objects, which specifies: The Schema Object allows the definition of input and output data types. These types can be objects, but also primitives and arrays.
So that leaves us with the required constraint, which I believe should still be added to the model where the primitive reference is used. e.g.:
"myModel": {
"type": " object",
"title": "MyModel",
"required": [
"myPeriod"
],
"properties": {
"myPeriod": {
"$ref": "#/definitions/Period"
},
"otherPeriod": {
"$ref": "#/definitions/Period"
}
}
}
Well, did I do a wrong mapping? Currently, I mapped a definition to a model directly, then, for a primitive model, the pattern and required were lost, since the ModelImpl hasn't properties to hold them.
I'm curious how you did it. Can you show it to me in details?
I see, looking at the Model interface and it's implementations I see there is support for the ArrayModel for instance, but no direct support for primitive model definitions.
Could that be missing in the Swagger implementation, or do I really misunderstand the specs?
Just checked the spec. Seems it didn't require that.