Closed lippea closed 8 years ago
@lippea Can you try the following?
body: `quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto` (fixed)
If you want multiple lines, you should try using multi-line sample values:
+ Response 200 (application/json; charset=utf-8)
+ Attributes
- userId: 1 (number, fixed)
- id: 1 (number, fixed)
- title: `sunt aut facere repellat provident occaecati excepturi optio reprehenderit` (fixed)
- body (fixed)
- Sample
line 1
line 2
line 3
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "line 1\nline 2\nline 3\n"
}
@pksunkara I did try to add `` around the value, but it didn' twork
@kylef I tried to use sample, but it seems the generated schema doesn't have enum for body at all. BTW, the \n is in the value, not because I want it to be multiple lines.
The schema generated for the MSON value I've shown above is the following:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"userId": {
"type": "number",
"enum": [
1
]
},
"id": {
"type": "number",
"enum": [
1
]
},
"title": {
"type": "string",
"enum": [
"sunt aut facere repellat provident occaecati excepturi optio reprehenderit"
]
},
"body": {
"type": "string"
}
},
"required": [
"userId",
"id",
"title",
"body"
]
}
So, here the sample value is not being used in the JSON schema. I've filed a bug on Drafter, our API Blueprint parser (https://github.com/apiaryio/drafter/issues/343).
@lippea Using \n
inside a string is not translated to a new line. \n
is literally just backslash and n, throughout the MSON specification there is no mention that this is possible.
The only way to include new lines in values is as I have shown above (multi-line sample values).
Closing issue, underlying problem with sample values in JSON Schema not being rendered correctly is now tracked via https://github.com/apiaryio/drafter/issues/343.
@kylef Does the underlying change fix the issue that backlash in MSON is translated into double backlash in schema?
@lippea Nope, that is not a bug. \n
in an MSON strings should not be converted to a new line, there is no mention of that in the MSON specification.
What https://github.com/apiaryio/drafter/issues/343 does is make it possible to use fixed multi-line sample values, so the following will work and produce the expected JSON Schema:
+ Attributes
+ userId: 1 (number, fixed)
+ id: 1 (number, fixed)
+ title: `sunt aut facere repellat provident occaecati excepturi optio reprehenderit` (fixed)
+ body (fixed)
+ Sample
quia et suscipit
suscipit recusandae consequuntur expedita et cum
reprehenderit molestiae ut ut quas totam
nostrum rerum est autem sunt rem eveniet architecto
If you're looking for literally placing \n
in the string, then it should be escaped in JSON Schema, as JSON requires escaping of backslashes, when you JSON decode the schema \\n
will be converted to \n
.
>>> json.loads('"Hello \\n world"')
'Hello \n world'
@kylef it seems not related to \n, but to . \n is translated to \n, and I have another case that \" is translated to \". \" is used to escape double quote in json response.
@kylef it seems fine now after I removed the \ in MSON. And \ is added in the generated schema to escape " in json
Okay, have you solved your problem?
I want to clarify that the following (using multi line sample):
# Fake Online REST API
## Group Posts
### /posts/1
#### Retrieve one post [GET]
+ Relation: posts-getposts
+ Response 200 (application/json; charset=utf-8)
+ Attributes
- userId: 1 (number, fixed)
- id: 1 (number, fixed)
- title: `sunt aut facere repellat provident occaecati excepturi optio reprehenderit` (fixed)
- body (fixed)
- Sample
quia et suscipit
suscipit recusandae consequuntur expedita et cum
reprehenderit molestiae ut ut quas totam
nostrum rerum est autem sunt rem eveniet architecto
Results in the following JSON Schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"userId": {
"type": "number",
"enum": [
1
]
},
"id": {
"type": "number",
"enum": [
1
]
},
"title": {
"type": "string",
"enum": [
"sunt aut facere repellat provident occaecati excepturi optio reprehenderit"
]
},
"body": {
"type": "string",
"enum": [
"quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
]
}
},
"required": [
"userId",
"id",
"title",
"body"
]
}
Given the changes at https://github.com/apiaryio/drafter/issues/343, which seem to be exactly what you're looking for. Please correct me if I'm wrong here.
@kylef It looks like what the top case needed. Thanks!
I tried to run "dredd get-one-post.apib http://jsonplaceholder.typicode.com/" with below blueprint with mson, but it failed.
This seems to be mson's issue because I tried same command with blueprint with generated schema and it worked.