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

Requirements get lost when model is made a definition #2

Open
momania opened this issue Sep 29, 2016 · 5 comments
Open

Requirements get lost when model is made a definition #2

momania opened this issue Sep 29, 2016 · 5 comments

Comments

@momania
Copy link

momania commented Sep 29, 2016

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.

@tminglei
Copy link
Owner

tminglei commented Oct 2, 2016

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.

@momania
Copy link
Author

momania commented Oct 3, 2016

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" 
    }
  }
}

@tminglei
Copy link
Owner

tminglei commented Oct 3, 2016

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?

@momania
Copy link
Author

momania commented Oct 3, 2016

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?

@tminglei
Copy link
Owner

tminglei commented Oct 4, 2016

Just checked the spec. Seems it didn't require that.

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

2 participants