Goland - generate type definition from JSON schema - goland

I have a both a example JSON file and a JSON schema. Is there any functionality in JetBrains GoLand IDE to generate the type definition given either of these files.
e.g. I have data.json:
{
"title": "hello",
"views": 45
}
and data-schema.json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"title": {
"type": "string"
},
"views": {
"type": "integer"
}
},
"required": [
"title",
"views"
]
}
Is their functionality / extension to generate this type definition from the given files:
type Obj1 struct {
Title string `json:"title"`
Views int `json:"views"`
}

This is possible since version 2021.1-
https://blog.jetbrains.com/go/2021/01/29/goland-2021-1-eap-begins/#work_with_json
Invoke Add key to tags on the struct or on any field in the struct via Alt+Enter. GoLand adds a tag to all fields in the struct with field names.

Related

Access definitions on ref property with newtonsoft.json.schema

I want to have access to the definitions in the schema in order to get the naming of the definition. I am using newtonsoft.json v11.01
I am building a c# converter for jsonschema to make a syntaxtree and compile it in order to get a typed version of the object at runtime.
{
"$id": "https://example.com/arrays.schema.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "xml remarks",
"type": "object",
"properties": {
"fruits": {
"type": "array",
"items": {
"type": "object",
"title": "fruit",
"required": ["naam"],
"properties": {
"naam": {
"type": "string",
"description": "The name of the fruit."
}
}
}
},
"vegetables": {
"type": "array",
"items": { "$ref": "#/definitions/veggie" }
}
},
"definitions": {
"veggie": {
"type": "object",
"required": [ "veggieName", "veggieLike" ],
"properties": {
"veggieName": {
"type": "string",
"description": "The name of the vegetable."
},
"veggieLike": {
"type": "boolean",
"description": "Do I like this vegetable?"
}
}
}
}
}
in the schema a reference is created with the name veggie. This is used in the property vegetable with a reference.
Json schema contains a definition on the root object but is doesn't have it on the property element. On the property element there is nothing identifiable to point to the right definition.
how do i find the right definition for the property?
In general, in order to resolve a json-pointer (a $ref is a "uri-reference", and the part after the # is a "json-pointer"), you need to have access to the root of the json document.
So if you currently have a function that only gets an argument that points to the "properties" section, then you need to give that function a second argument that points to the root of the document.
(It gets more complicated when you're using a schema made up of more than one file; then you need a second argument that points to the roots of all the schema documents)
It's one of the more difficult parts of writing software that interprets json-schema files, especially if your language/library doesn't have support for json-pointers built-in - you'll need to write it yourself in that case.

Is there a way to support JSON schema references in Paw?

Paw has good tools for documenting simple APIs, but now I need to do something more complicated and I would like to use JSON Schema references.
For example, like the following snippet from the Swagger 2.0 examples, where the definitions are presented later in the document:
"post": {
"description": "Creates a new pet in the store. Duplicates are allowed",
"operationId": "addPet",
"parameters": [
{
"name": "pet",
"in": "body",
"description": "Pet to add to the store",
"required": true,
"schema": {
"$ref": "#/definitions/NewPet"
}
}
],
...
Is there a way to to this in Paw?

How to filter by nested attributes in JSONAPI?

Assuming we have the following data structure
"data": [
{
"type": "node--press",
"id": "f04eab99-9174-4d00-bbbe-cdf45056660e",
"attributes": {
"nid": 130,
"uuid": "f04eab99-9174-4d00-bbbe-cdf45056660e",
"title": "TITLE OF NODE",
"revision_translation_affected": true,
"path": {
"alias": "/press/title-of-node",
"pid": 428,
"langcode": "es"
}
...
}
The data returned is compliant with JSON API standards, and I have no problem retrieving and processing it, except for the fact that I need to be able to filter the nodes returned by the path pid.
How can I filter my data by path.pid?
I have tried:
- node-press?filter[path][pid]=428
- node-press?filter[path][pid][value]=428
to no avail
It's not well defined in the filters section of the specification but other parameters such as include describe accessing nested keys with dot-notation. You could try ?filter[path.pid]=428 and parse the filter that way.
"field_country": {
"data": {
"type": "taxonomy_term--country",
"id": "818f11ab-dd9d-406b-b1ca-f79491eedd73"
}
}
Above structure can be filtered by ?filter[field_country.id]=818f11ab-dd9d-406b-b1ca-f79491eedd73

Newtonsoft.Json.Schema how not generate references $ref in schema

Json.Net.Schema is generating refernces for all of my string arrays. for example this c#
public string[] ovoImageUrl;
public string[] ovoMetaprofile;
produces this Json schema
"ovoImageUrl": {
"type": [
"array",
"null"
],
"items": {
"type": [
"string",
"null"
]
}
},
"ovoMetaprofile": {
"$ref": "#/properties/ovoImageUrl"
},
Since I am using the Json Schema as human readable documentation this is not desirable. is there any way, maybe with attributes, stop these "$ref" from being created?
Cheers,
Grant
The latest version of Newtonsoft.Json.Schema has a SchemaReferenceHandling setting on JSchemaGenerator for controlling references.
JSchemaGenerator generator = new JSchemaGenerator();
generator.SchemaReferenceHandling = SchemaReferenceHandling.None;
JSchema schema = generator.Generate(typeof(Person));
http://www.newtonsoft.com/jsonschema/help/html/P_Newtonsoft_Json_Schema_Generation_JSchemaGenerator_SchemaReferenceHandling.htm

JsonSchemaGenerator adding Minimum

I'm trying to generate a schema using Newtonsoft JsonSchemaGenerator similar to the following schema
{
"title": "Example Schema",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
}
I'm trying to create a JsonConverter and set it to the ContractResolver but I can not find a way to bind it to the specific property.
I am tracing the code and I see the GenerateInternal on JsonSchemaGenerator (via GenerateObjectSchema) is looping through Contract.Properties and recursively calling itself but only passing property.PropertyType to itself.
foreach (JsonProperty property in contract.Properties)
{
. . .
JsonSchema propertySchema = GenerateInternal(property.PropertyType, property.Required, !optional);
. . .
}
private JsonSchema GenerateInternal(Type type, Required valueRequired, bool required)
So if I have a class with 2 integer properties with different ranges it won't be able to create different schemas for each.

Resources