如何在 Json 模式响应中重命名 class
how to rename class in Json schema response
我有 class,我正在从中生成模式。
public class CreateDemoRequest
{
[JsonIgnore]
public int Id { get; set; }
[DataMember(EmitDefaultValue = false, Name = "models")]
[XmlAttribute(AttributeName = "models")]
[JsonProperty(PropertyName = "models"), NullValueHandling = NullValueHandling.Ignore)]
public DemoRequestModel[] Models { get; set; }
}
public class DemoRequestModel{
[DataMember(EmitDefaultValue = false, Name = "demoField ")]
[XmlAttribute(AttributeName = "demoField ")]
[JsonProperty(PropertyName = "demoField ", NullValueHandling = NullValueHandling.Ignore)]
public string DemoField { get; set; }
}
上面是 class,下面是生成架构的代码。
JSchema schemaResult = new JSchema();
JSchemaGenerator schemaGen = _schemaGenerator.Value;
schemaResult = schemaGen.Generate(typeof(CreateDemoRequest));
并得到如下响应。
{
"_schema": {
"definitions": {
"DemoRequestModel": {
"properties": {
"demoField": {
"type": [
"string",
"null"
]
}
}
}
},
"properties": {
"models": {
"type": "array",
"items": {
"$ref": "#/definitions/DemoRequestModel"
}
}
}
}
}
如何在定义下获得“模型”而不是“DemoRequestModel”?
尝试使用数据合同但没有用。
您的代码存在一些问题,例如 Models
对 JsonProperty
的错误使用,以及使用 typeof(...)
.
未正确调用 JSchemaGenerator.Generate
这些问题已解决,为什么您要获得 models
而不是 DemoRequestModel
?
这给了你想要的,但为什么呢?
public class CreateDemoRequest
{
[JsonIgnore]
public int Id { get; set; }
[JsonProperty(PropertyName = "models", NullValueHandling = NullValueHandling.Ignore)]
public models[] Models { get; set; }
}
public class models
{
[JsonProperty(PropertyName = "demoField", NullValueHandling = NullValueHandling.Ignore)]
public string DemoField { get; set; }
}
输出:
{
"definitions": {
"models": {
"type": [
"object",
"null"
],
"properties": {
"demoField": {
"type": [
"string",
"null"
]
}
}
}
},
"type": "object",
"properties": {
"models": {
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/models"
}
}
}
}
我有 class,我正在从中生成模式。
public class CreateDemoRequest
{
[JsonIgnore]
public int Id { get; set; }
[DataMember(EmitDefaultValue = false, Name = "models")]
[XmlAttribute(AttributeName = "models")]
[JsonProperty(PropertyName = "models"), NullValueHandling = NullValueHandling.Ignore)]
public DemoRequestModel[] Models { get; set; }
}
public class DemoRequestModel{
[DataMember(EmitDefaultValue = false, Name = "demoField ")]
[XmlAttribute(AttributeName = "demoField ")]
[JsonProperty(PropertyName = "demoField ", NullValueHandling = NullValueHandling.Ignore)]
public string DemoField { get; set; }
}
上面是 class,下面是生成架构的代码。
JSchema schemaResult = new JSchema();
JSchemaGenerator schemaGen = _schemaGenerator.Value;
schemaResult = schemaGen.Generate(typeof(CreateDemoRequest));
并得到如下响应。
{
"_schema": {
"definitions": {
"DemoRequestModel": {
"properties": {
"demoField": {
"type": [
"string",
"null"
]
}
}
}
},
"properties": {
"models": {
"type": "array",
"items": {
"$ref": "#/definitions/DemoRequestModel"
}
}
}
}
}
如何在定义下获得“模型”而不是“DemoRequestModel”? 尝试使用数据合同但没有用。
您的代码存在一些问题,例如 Models
对 JsonProperty
的错误使用,以及使用 typeof(...)
.
JSchemaGenerator.Generate
这些问题已解决,为什么您要获得 models
而不是 DemoRequestModel
?
这给了你想要的,但为什么呢?
public class CreateDemoRequest
{
[JsonIgnore]
public int Id { get; set; }
[JsonProperty(PropertyName = "models", NullValueHandling = NullValueHandling.Ignore)]
public models[] Models { get; set; }
}
public class models
{
[JsonProperty(PropertyName = "demoField", NullValueHandling = NullValueHandling.Ignore)]
public string DemoField { get; set; }
}
输出:
{
"definitions": {
"models": {
"type": [
"object",
"null"
],
"properties": {
"demoField": {
"type": [
"string",
"null"
]
}
}
}
},
"type": "object",
"properties": {
"models": {
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/models"
}
}
}
}