[IMPLEMENTED] Swagger/NSwag - Localization fields Dictionary

Hello Sebastian,

I try to use NSwag to generating API client. But if I use code base on what Squidex do for its own client generator I end up with something like

    public partial class TestDto 
    {
        public TestNameProperty Test { get; set; }
    }

    public partial class TestNameProperty 
    {
        public string En { get; set; }

        public string CsCZ { get; set; }
    }

Even if NSwag at least base on what I found should support dictionary. Problem is, that Squidex do not generate swagger file “correctly”.

What i got in swagger file generated by squidex.

    ...

    "TestDto": {

     "test": {
                "description": "test",
                "nullable": true,
                "$ref": "#/components/schemas/TestNameProperty"
              },

    ..

    ..

    "TestNameProperty": {
            "type": "object",
            "properties": {
              "en": {
                "type": "string",
                "description": "English",
                "nullable": true
              },
              "cs-CZ": {
                "type": "string",
                "description": "Czech (Czechia)",
                "nullable": true
              }
              ..
          }...

I think, based on the docs here https://swagger.io/docs/specification/data-models/dictionaries/ it should use additionalProperties.

Testing example which I try with NSwag studio

    {
    "openapi": "3.0.0",
    "info": {
        "title": "Squidex API for test",
        "version": "5.5.0.0",
        "x-logo": {
        "url": "https://test.local/images/logo-white.png",
        "backgroundColor": "#3f83df"
        }
    },
    "servers": [
        {
        "url": "https://test.local/api"
        }
    ],
    "paths": {
        "/content/test/items/": {
        "get": {
            "responses": {
            "200": {
                "content": {
                "application/json": {
                    "schema": {
                    "type": "object",
                    "properties": {
                        "test-dictionary": {
                        "type": "object",
                        "additionalProperties": {
                            "type": "string"
                        }
                        },
                        "test-compose-object": {
                        "type": "object",
                        "additionalProperties": {
                            "type": "object",
                            "properties": {
                            "code": {
                                "type": "integer"
                            },
                            "text": {
                                "type": "string"
                            }
                   ...

And output:

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.3.9.0 (Newtonsoft.Json v12.0.0.0)")]
    public partial class Response 
    {
        [Newtonsoft.Json.JsonProperty("test-dictionary", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public System.Collections.Generic.IDictionary<string, string> TestDictionary { get; set; }

        [Newtonsoft.Json.JsonProperty("test-compose-object", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        public System.Collections.Generic.IDictionary<string, Anonymous> TestComposeObject { get; set;    }
        ..
    }

I open it here because this change also mostly likely introduces BC. And also, I am not sure if it is any reason to current behavior. But I think this could be useful.

I think this makes a lot of sense :slight_smile: … Can you provide a PR? Btwe: I also provide a flatdata OpenAPI spec now.

The code is here: https://github.com/Squidex/squidex/blob/master/backend/src/Squidex.Domain.Apps.Core.Operations/GenerateJsonSchema/JsonSchemaExtensions.cs#L40

Last hour or so I try to dig in to the NJsonSchema but I am not familiar with it and it seems like it is not very well documented (honestly it is real pain to try to work with this). I am not sure if I will be able to provide PR to this.

In the code I added you just have to set the AdditionalPropertiesSchema to the schema. So something like:

partitionObject.AdditionalPropertiesSchema = JsonTypeVisitor.BuildProperty(field, schemaResolver, withHidden);

I jguess you can also remove the loop then.

PR is ready but I am not able to run squidex locally (i cannot directly try this change). I fix test which directly use changed method but I am not sure if there is another tests (Integration tests which probably required some dependencies).

edit: Ok that was quick it fails I check it tomorrow.

If you cannot run it locally, I can have a look as well. I asked because I thought you can have a look to the generated code as well.

I do not why but a I cannot run squidex frontend. Not sure if it because i maybe update node from last time. I try to remove node_modules and package-lock but i end up with

npm install
npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR!
npm ERR! While resolving: squidex@1.0.0
npm ERR! Found: zone.js@0.11.3
npm ERR! node_modules/zone.js
npm ERR!   zone.js@"0.11.3" from the root project
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer zone.js@"~0.10.3" from @angular/core@11.0.0
npm ERR! node_modules/@angular/core
npm ERR!   @angular/core@"11.0.0" from the root project
npm ERR!   peer @angular/core@">=2.3.1 <12.0.0 || ^11.0.0-next || ^11.1.0-next || ^11.2.0-next" from codelyzer@6.0.1
npm ERR!   node_modules/codelyzer
npm ERR!     dev codelyzer@"6.0.1" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!

I am not sure but I think something is change with last nodejs/npm

Strange, have never seen that. My build server pulls regularly (whenever it runs out of disk) so I should have seen it there.

But I have the PR ready (with more issues that I have found):

1 Like

This topic was automatically closed after 2 days. New replies are no longer allowed.