Newtonsoft.Json.Schema неправильно проверяет полезную нагрузку JSON по схеме JSON.C#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Newtonsoft.Json.Schema неправильно проверяет полезную нагрузку JSON по схеме JSON.

Сообщение Anonymous »

Newtonsoft.Json.Schema неправильно проверяет входные данные JSON по схеме JSON
В приведенном ниже примере проверка схемы возвращает true для приведенной ниже полезной нагрузки JSON, хотя:

[*]для «mCRO» значение NULL равно false
[*]iNumber» имеет varchar(10)
< /ul>
Почему эти ошибки не обнаруживаются?
Для проверки мы используем Newtonsoft.Json.Schema.
Схема JSON:

Код: Выделить всё

{
"openapi": "3.0.2",
"info": {
"title": "Demo API",
"version": "1.1.1",
"description": "Demo version"
},
"tags": [
{
"name": "Records",
"description": "Registration"
}
],
"paths": {
"/records/AddUpdateRecord": {
"post": {
"tags": [
"Records"
],
"description": "Add or Update a Object",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/addUpdateRecords"
}
}
}
},
"responses": {
"200": {
"description": "OK - Indicates that the request has succeeded.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/httpResponse200"
}
}
}
},
"400": {
"description": "Bad Request",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/httpResponse400"
}
}
}
},
"401": {
"description": "Unauthorized "
},
"403": {
"description": "Forbidden - Unauthorized request."
},
"429": {
"description": "Too Many Requests "
},
"500": {
"description": "Internal Server Error "
}
}
}
},
"/records/AddUpdateC": {
"post": {
"tags": [
"Records"
],
"description": "Add or Update existing 1 to N Coverages.",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/addUpdateC"
}
}
}
},
"responses": {
"200": {
"description": "OK - Indicates that the request has succeeded.",
"content": {
"application/json": {
"schema": {
"$ref":  "#/components/schemas/httpResponse200"
}
}
}
},
"400": {
"description": "Bad Request ",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/httpResponseC400"
}
}
}
},
"401": {
"description": "Unauthorized ."
},
"403": {
"description": "Forbidden."
},
"429": {
"description": "Too Many Requests "
},
"500": {
"description": "Internal Server Error "
}
}
}
},
"/records/UpdateRO": {
"put": {
"tags": [
"Records"
],
"description": "Update an existing RO.",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/updateRO"
}
}
}
},
"responses": {
"200": {
"description": "OK - Indicates that the request has succeeded.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/httpResponse200"
}
}
}
},
"400": {
"description": "Bad Request ",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/httpResponse400"
}
}
}
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden ."
},
"429": {
"description": "Too Many Requests ."
},
"500": {
"description": "Internal Server Error "
}
}
}
},
"/records/RemoveRO": {
"post": {
"tags": [
"Records"
],
"description": "Remove an existing RO",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/removeRO"
}
}
}
},
"responses": {
"200": {
"description": "OK - Indicates that the request has succeeded.",
"content":  {
"application/json": {
"schema": {
"$ref": "#/components/schemas/httpResponse200"
}
}
}
},
"400": {
"description": "Bad Request.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/httpResponse400"
}
}
}
},
"401": {
"description": "Unauthorized."
},
"403": {
"description": "Forbidden"
},
"429": {
"description": "Too Many Requests ."
},
"500": {
"description": "Internal Server Error"
}
}
}
},
"/records/MoveP": {
"post": {
"tags": [
"Records"
],
"description": "Move",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/moveP"
}
}
}
},
"responses": {
"200": {
"description": "OK - Indicates that the request has succeeded.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/httpResponse200"
}
}
}
},
"400": {
"description": "Bad Request.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/httpResponse400"
}
}
}
},
"401": {
"description": "Unauthorized ."
},
"403": {
"description": "Forbidden "
},
"429": {
"description": "Too Many Requests ."
},
"500": {
"description": "Internal Server Error."
}
}
}
}
},
"components": {
"schemas": {
"httpResponse200": {
"type": "object",
"properties": {
"results": {
"nullable": true,
"example": null
},
"requestGuid": {
"type": "string",
"example": "GUI"
},
"errors": {
"nullable": true,
"example":  null
},
"httpStatusCode": {
"type": "integer",
"nullable": false,
"example": 200
}
}
},
"addUpdateC": {
"type": "object",
"properties": {
"iCR": {
"type": "array",
"description": "1 to N",
"items": {
"type": "object",
"properties": {
"requestGuid": {
"type": "string",
"example": "GUI",
"nullable": false
},
"iNumber": {
"type": "string",
"format": "varchar(10)",
"nullable": false
},
"eTypeRequest": {
"type": "array",
"description": "1 to N",
"items": {
"type": "object",
"properties": {
"requestGuid": {
"type": "string",
"example": "16-byte binary value",
"nullable": false
},
"eTypeECode": {
"type": "string",
"format": "varchar(3)",
"nullable": false,
"description": "List"
}
}
}
}
}
}
}
}
},
"moveP": {
"type": "object",
"properties": {
"iMPR": {
"type": "object",
"properties": {
"requestGuid": {
"type": "string",
"example": "GUID",
"nullable": false
},
"iNumber": {
"type": "string",
"format": "varchar(10)",
"nullable": false
}
}
}
}
},
"removeRO": {
"type": "object",
"properties": {
"iRROR": {
"type": "object",
"properties": {
"requestGuid": {
"type": "string",
"example": "GUID",
"nullable": false
},
"iNumber": {
"type": "string",
"format": "varchar(10)",
"nullable": false
},
"mCRO": {
"type": "number",
"format":  "smallint",
"nullable": false,
"description": "List"
}
}
}
}
},
"addUpdateRecords": {
"type": "object",
"properties": {
"iROR": {
"type": "object",
"properties": {
"requestGuid": {
"type": "string",
"example": "GUID",
"nullable": false
},
"iNumber": {
"type": "string",
"format": "varchar(10)",
"nullable": false
},
"mCRO": {
"type": "number",
"format": "smallint",
"nullable": false,
"description": "List"
}
}
},
"iCR": {
"type": "array",
"description": "1 to N",
"items": {
"type": "object",
"properties": {
"requestGuid": {
"type": "string",
"example": "16-byte binary value",
"nullable": false
},
"iNumber": {
"type": "string",
"format": "varchar(10)",
"nullable": false
},
"eTypeRequest": {
"type": "array",
"description": "1 to N",
"items": {
"type": "object",
"properties": {
"requestGuid": {
"type": "string",
"example": "16-byte binary value",
"nullable": false
},
"eTypeECode": {
"type": "string",
"format": "varchar(3)",
"nullable": false,
"description": "List"
}
}
}
}
}
}
}
}
},
"updateRO": {
"type": "object",
"properties": {
"iROR": {
"type": "object",
"properties": {
"requestGuid": {
"type": "string",
"example": "GUID",
"nullable": false
},
"iNumber": {
"type": "string",
"format": "varchar(10)",
"nullable": false
},
"mCRO": {
"type": "number",
"format": "smallint",
"nullable": false,
"description": "List"
}
}
}
}
},
"httpResponse400":  {
"type": "object",
"properties": {
"results": {
"nullable": true,
"example": null
},
"requestGuid": {
"type": "string",
"example": "GUI"
},
"errors": {
"type": "array",
"items": {
"type": "object",
"properties": {
"cTypeCode": {
"nullable": true,
"example": null
},
"cFormula": {
"nullable": true,
"example": null
},
"errorDescription": {
"type": "string"
}
}
}
},
"httpStatusCode": {
"type": "integer",
"nullable": false,
"example": 400
}
}
},
"httpResponseC400": {
"type": "object",
"properties": {
"results": {
"nullable": true,
"example": null
},
"requestGuid": {
"type": "string",
"example": "GUI "
},
"errors": {
"type": "array",
"items": {
"type": "object",
"properties": {
"cTypeCode": {
"nullable": true,
"example": null
},
"cFormula": {
"nullable": true,
"example": null
},
"errorDescription": {
"type": "string"
}
}
}
},
"httpStatusCode": {
"type": "integer",
"nullable": false,
"example": 400
}
}
}
}
}
}
Полезная нагрузка JSON:

Код: Выделить всё

{
"iRROR": {
"requestGuid": "GUID",
"iNumber": "12345678910111213",
"mCRO": null
}
}
Код C#:

Код: Выделить всё

using (StreamReader file = File.OpenText(@"C:\\Sample\\OpenAPISpecification.json"))
using (JsonTextReader reader = new JsonTextReader(file))
{
JSchema schema = JSchema.Load(reader);
JObject json = JObject.Parse(File.ReadAllText(@"C:\\Sample\\iRROR.json"));

//validate json
IList errors;
bool valid = json.IsValid(schema, out errors);

new ValidateResponse
{
Valid = valid,
Errors = errors
};

}

Почему эти ошибки не обнаруживаются?
Для проверки мы используем Newtonsoft.Json.Schema.>

Подробнее здесь: https://stackoverflow.com/questions/786 ... t-a-json-s
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»