fabien0102 / openapi-codegen

A tool for generating code base on an OpenAPI schema.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[bug] if the schema name has '.' in the name then throws: Internal Error: #/components/schemas/Name.With.Dots not found!

ci-vamp opened this issue · comments

good news, its a simple fix. i tested it out directly in the lib source itself and got it working.

the issue arises from this line

what is happening is that a model with . in its name (something ive inherited in an internal API as a resolution to schema name conflicts) is treated by the lodash.get method as a path traversal.

so for example Name.With.Dots as the model name would be searching for an object looking like this

{
  components: {
    schemas: {
      Name: {
        With: {
          Dots: ...
        }
      }
    }
  }
}

when in reality the actual target is

{
  components: {
    schemas: {
      'Name.With.Dots': ...
    }
  }
}

the fix uses the default result argument of lodash.get to see if it can be found through direct access. i did some testing to see if there is ever a case where components.schema is undefined and didnt find any but to protect against a case others might encounter we do some undefined / null checking:

export const getReferenceSchema = (
  $ref: string,
  openAPIDocument: Pick<OpenAPIObject, "components">
): SchemaObject => {
  const [hash, ...refPath] = $ref.split("/");
  if (hash !== "#") {
    throw new Error("This library only resolve local $ref");
  }

  // refPath.at(-1) is the actual name being searched for, where [0] is 'components' and [1] is 'schemas'
  const defaultDirectSearch = openAPIDocument?.components?.schemas && openAPIDocument.components.schemas[refPath.at(-1)];
  const referenceSchema = get(openAPIDocument, refPath.join("."), defaultDirectSearch);

  if (!referenceSchema) {
    throw new Error(`${$ref} not found!`);
  }

  ...
};

Hi, same issue, hope it merge soon