Twipped / joi-to-swagger

A library to convert Joi schema objects into Swagger schema definitions
Other
164 stars 63 forks source link

Generation of components using .pattern() #95

Closed GabrielPadureanu closed 2 years ago

GabrielPadureanu commented 2 years ago

Hi,

I'm using as a value of a key a pattern with a schema object.

I would expect that a component is created for the schema referenced in the additional parameters.

const innerSchema = Joi.object().keys({
    key: Joi.any(),
  }).meta({ className: 'innerSchema' });
    id: Joi.object().pattern(
      /^/,
      innerSchema,
    ),
  }).meta({ className: 'patternSchema' });

const componentsPatternSchema = j2s(patternSchema as Schema).components; componentsPatternSchema is

{
  "schemas": {
    "patternSchema": {
      "type": "object",
      "properties": {
        "id": {
          "type": "object",
          "properties": {},
          "additionalProperties": {
            "$ref": "#/components/schemas/innerSchema"
          }
        }
      },
      "additionalProperties": false
    }
  }
}

but no component was created for what is referenced in the additonalProperties.

Using a schema as a value for a key of a schema will create the component as expected

const noPatternSchema = Joi.object().keys({ id: innerSchema }).meta({ className: 'noPatternSchema' });
  const componentsNoPatternSchema = j2s(noPatternSchema as Schema).components;

componentsNoPatternSchema is

  "schemas": {
    "innerSchema": {
      "type": "object",
      "properties": {
        "key": {}
      },
      "additionalProperties": false
    },
    "noPatternSchema": {
      "type": "object",
      "properties": {
        "id": {
          "$ref": "#/components/schemas/innerSchema"
        }
      },
      "additionalProperties": false
    }
  }
}

Is this an issue or another approach is required in order to get the expected output?

Thank you for any insight provided!

joi-to-swagger version 6.1.0

Mairu commented 2 years ago

I think that was an oversight when #94 was implemented. Should be fixed now in 6.1.1.

GabrielPadureanu commented 2 years ago

Thank you for your prompt answer and solution!