OpenAPITools / OpenAPITools/openapi-generator

Bug in InlineModelResolver

Open
#3,580 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

Logic of flattenComponents method is out of sequence. ArraySchema are not flattened.

openapi-generator version

4.0.0, but noted also in master

OpenAPI declaration file content or url
    "products":{
      "type":"array",
      "title":"List of Prodcuts",
      "description":"List of Products",
      "xml":{
        "wrapped":true
      },
      "items":{
        "type":"object",
        "title":"Product",
        "description":"Product",
        "xml":{
          "name":"product",
          "wrapped":true
        },
        "required":[
          "id",
          "qty"
        ],
        "properties":{
          "id":{
            "type":"string",
            "title":"Product Id",
            "description":"Master Product Id"
          },
          "qty":{
            "type":"number",
            "format":"float",
            "title":"Quantity",
            "description":"Number of this product being shipped at this line",
            "minimum":0.1
          },
          "freightClass":{
            "type":"string",
            "title":"Freight Class",
            "description":"Freight Class"
          },
          "weight":{
            "type":"number",
            "format":"float",
            "title":"Weight",
            "description":"Weight of single unit of measure of product"
          },
          "weightUnit":{
            "type":"string",
            "title":"Weight Unit",
            "description":"Weight Unit"
          },
          "dimLength":{
            "type":"number",
            "format":"float",
            "title":"Dimension, Length",
            "description":"Length of the product to be shipped"
          },
          "dimHeight":{
            "type":"number",
            "format":"float",
            "title":"Dimension, Height",
            "description":"Height of the product to be shipped"
          },
          "dimWidth":{
            "type":"number",
            "format":"float",
            "title":"Dimension, Width",
            "description":"Width of the product to be shipped"
          },
          "dimUnits":{
            "type":"string",
            "title":"Dimension, unit of measure",
            "description":"Unit of measure of dimensions"
          }
        }
      }
    }
Command line used for generation

N/A (Mojo)

Steps to reproduce
  1. #/component/schema/ProductList is type "array"
  2. Array items are an object with properties

The array item is not flattened to a $ref.

Related issues/PRs
Suggest a fix/enhancement

Since an ArraySchema is also a Schema, this sequence doesn't work.
} else if (model instanceof Schema) {
Schema m = (Schema) model;
Map<String, Schema> properties = m.getProperties();
flattenProperties(properties, modelName);
fixStringModel(m);
} else if (ModelUtils.isArraySchema(model)) {

Use this sequence instead (unless the logic for Schema should also be executed, in which case the "else" should be removed from the ArraySchema condition):
} else if (ModelUtils.isArraySchema(model)) {
final ArraySchema m = (ArraySchema) model;
final Schema inner = m.getItems();
if (inner instanceof ObjectSchema) {
System.out.printf("--- %s: found inner schema for array items%n", modelName);
final ObjectSchema op = (ObjectSchema) inner;
if (op.getProperties() != null && op.getProperties().size() > 0) {
System.out.printf("--- %s: inner schema has properties%n", modelName);
final String innerModelName = resolveModelName(op.getTitle(), modelName + "_inner");
final Schema innerModel = modelFromProperty(op, innerModelName);
final String existing = matchGenerated(innerModel);
if (existing == null) {
openAPI.getComponents().addSchemas(innerModelName, innerModel);
addGenerated(innerModelName, innerModel);
final Schema schema = new Schema().$ref(innerModelName);
schema.setRequired(op.getRequired());
m.setItems(schema);
} else {
final Schema schema = new Schema().$ref(existing);
schema.setRequired(op.getRequired());
m.setItems(schema);
}
}
}
} else if (model instanceof Schema) {

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at InlineModelResolver.flattenComponents and reproduce the issue with the provided OpenAPI declaration, focusing on the products array and its object items. Verify that the array item is flattened to a $ref while preserving the reported schema details; the payload does not name a test file.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.