strapi icon indicating copy to clipboard operation
strapi copied to clipboard

strapi-plugin-documentation: Schema value constraints not properly expressed

Open doro0020 opened this issue 3 years ago • 3 comments

Bug report

Describe the bug

Some of the declared schema constraints are not expressed by strapi-plugin-documentation's documentation.

Specific fields

The missing constraints are allowed by the OpenAPI schema (https://swagger.io/docs/specification/data-models/data-types/), except for the time format and unique field

Does not work:
  • Unique Field
  • Integer: min, max
  • Email: treated as string
  • Text: RegExp
  • Time: treated as string
Works:
  • Required Field
  • Boolean type
  • Date type
  • Date-time type
  • Text: MinLength:
  • Text: MaxLength:
  • Enum: types, default

Steps to reproduce the behavior

  1. Administrator management GUI, Content-Types builder
  2. Create a new collection type with a number field, with minimum and maximum values
  3. Create an email field
  4. Create a text field with a regular expression constraint
  5. Go to extensions/documentation/documentation/1.0.0/full_documentation.json
  6. Check created schema for newly created collection in "components" : { "schemas" : {} }

Expected behavior

The field(s) with constraints should be marked with the assigned formats and/or constraints

Code snippets

Observed Behavior

Schema snippet from full_documentation.json, refer to Code Snippet - Settings for exact model settings

  • The minimum and maximum constraints for Between3and8 are not expressed
  • The email has no format.
  • There is no regex pattern.
	"Constraint-test": {
        "required": [
          "id",
          "Between3and8",
          "EmailTest",
          "TextTest"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "Between3and8": {
            "type": "integer"
          },
          "EmailTest": {
            "type": "string"
          },
          "BooleanTest": {
            "type": "boolean"
          },
          "DateTest": {
            "type": "string",
            "format": "date"
          },
          "TextTest": {
            "type": "string",
            "maxLength": 10,
            "minLength": 5
          },
          "EnumTest": {
            "type": "string",
            "default": "Enum3",
            "enum": [
              "Enum1",
              "Enum2",
              "Enum3",
              "Enum4"
            ]
          },
          "TimeTest": {
            "type": "string"
          },
          "published_at": {
            "type": "string",
            "format": "date-time"
          }
        }
	}

Expected Behavior code

Based on OpenAPI schema used by Plugin Documentation


	"Constraint-test": {
        "required": [
          "id",
          "Between3and8",
          "EmailTest",
          "TextTest"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "Between3and8": {
            "type": "integer",
	    "minimum": 3,
	    "maximum": 8
          },
          "EmailTest": {
            "type": "string",
	    "format": "email"
          },
          "BooleanTest": {
            "type": "boolean"
          },
          "DateTest": {
            "type": "string",
            "format": "date"
          },
          "TextTest": {
            "type": "string",
            "maxLength": 10,
            "minLength": 5,
	    "pattern": "abc"
          },
          "EnumTest": {
            "type": "string",
            "default": "Enum3",
            "enum": [
              "Enum1",
              "Enum2",
              "Enum3",
              "Enum4"
            ]
          },
          "TimeTest": {
            "type": "string"
          },
          "published_at": {
            "type": "string",
            "format": "date-time"
          }
        }
	}

Settings

Settings are properly saved and displayed in api/constraint-test/models/constraint-test.settings.json, as well as the administrator GUI.

{
  "kind": "collectionType",
  "collectionName": "constraint_tests",
  "info": {
    "name": "ConstraintTest",
    "description": ""
  },
  "options": {
    "increments": true,
    "timestamps": true,
    "draftAndPublish": true
  },
  "attributes": {
    "Between3and8": {
      "type": "integer",
      "required": true,
      "max": 8,
      "min": 3
    },
    "EmailTest": {
      "type": "email",
      "required": true
    },
    "BooleanTest": {
      "type": "boolean"
    },
    "DateTest": {
      "type": "date"
    },
    "TextTest": {
      "type": "string",
      "maxLength": 10,
      "minLength": 5,
      "regex": "abc",
      "unique": true,
      "required": true
    },
    "EnumTest": {
      "type": "enumeration",
      "enum": [
        "Enum1",
        "Enum2",
        "Enum3",
        "Enum4"
      ],
      "default": "Enum3"
    },
    "TimeTest": {
      "type": "time"
    }
  }
}

System

  • Node.js version: v14.17.2
  • NPM version: 6.14.13
  • Strapi version: 3.6.6
  • Database: PostgreSQL 12.7 (on docker instance)
  • Operating system: Ubuntu 20.04.2 LTS

Additional context

@tokmakoff

doro0020 avatar Aug 11 '21 02:08 doro0020

It does appear that at least the min and max constraints were intended to be available but don't function, due to essentially a typo in getting "minimum", "maximum" instead of "min", "max", from the model

Demonstrated in a commit on our fork: 9c9a2466dd0d5b0f2434973fcff8b526673ccee7

There is even a typo in "maxmimun" https://github.com/strapi/strapi/blob/1fe4b5ecad162e487fa7dd26a9422569915b4d9c/packages/strapi-plugin-documentation/services/Documentation.js#L548-L549

Would a pull request of the other fixes raised by this issue be considered here?

doro0020 avatar Aug 17 '21 07:08 doro0020

I have a similar issue. My openapi.json is not valid acording to https://editor.swagger.io/. Because content fields and description fields are missing.

I solved this by editing the json on a custom api call:

      const rawDocumentation = fs.readFileSync(openAPISpecsPath, 'utf8');
      let documentation = JSON.parse(rawDocumentation);
    
      for (const path in documentation.paths) {
        for (const method in documentation.paths[path]) {
          documentation.paths[path][method].responses['200'].description = 'foo';
          documentation.paths[path][method].requestBody.content = {};
        }
      }

      return documentation;

which still leaves me a lot of semantic errors, but not structurel erros anymore..

martijn10kb avatar Dec 24 '21 13:12 martijn10kb

Hi @doro0020 thanks for submitting this issue.

Would a pull request of the other fixes raised by this issue be considered here?

Are you offering to open a PR? If so it would be very welcome and you could ping me to review it. Otherwise I will add this to the backlog and hopefully we can get to it soon.

markkaylor avatar Sep 15 '22 12:09 markkaylor

Hello,

In order to keep our GitHub issues clean and only related to bug reports I've moved your enhancement / feature requested over to our feedback database.

You can find your request here: https://feedback.strapi.io/developer-experience/p/strapi-plugin-documentation-schema-value-constraints-not-properly-expressed

Thanks!

derrickmehaffy avatar Feb 02 '23 20:02 derrickmehaffy