Skip to content

Pull all schemas instead of just those linked to paths #11

@dradetsky

Description

@dradetsky

This is a feature request.

The way spec2ts (and most comparable tools) work to generate types from schemas is:

  1. for each path in paths
  2. for each operation in path
  3. for the request & all responses, find the associated schema & emit a type

This is probably what the tool should do by default.

However, from my perspective, there are times when I'd rather just say: for every schema in components.schemas, emit a type. I can explain more about the cases where this is useful if desired.

For example, you could add a flag --all-schemas which would say to just emit a type for every schema in the target file, instead of trying to follow the paths.

If you were to do this, it would also be potentially useful to add the ability to not validate that the input file is valid openapi, although this is something that users could work around in the short term. The reason is that I would want to write a fragment with just my schemas and have the main file refer to it with jsonschema refs. But if I want to pass the file with just the schemas into spec2ts, i would have to write

openapi: 3.0.3

info:
  title: chicken
  version: 0.0.0

paths:

components:
  schemas:
    ThingKey:
      type: object
      properties:
        userId:
          type: integer
        thingId:
          type: integer
      required:
        - userId
        - thingId

    ThingData:
      type: object
      properties:
        lastSeen:
          type: string
          format: date-time
        value:
          type: integer
          
    Thing:
      type: object
      allOf:
        - $ref: '#/components/schemas/ThingKey'
        - $ref: '#/components/schemas/ThingData'

the main problem with this is that I'm repeating info.version (and I guess the openapi version) multiple times, so if I bump my api's version, I have to change it in multiple places. This is merely kind of ugly though, and not a major problem.

Another possible improvement would be for the user to specify the prefix under which all entries would be assumed to be schemas, which by default would be components.schemas. That way, the user could have a fragment with just a schema and no prefix (i think people do this a lot), and set the prefix to empty. This is also not very important. Personally, I always make my fragments start with the components.schemas prefix like the above example because it makes it obvious what these things are, but not everybody does this.


So, to summarize: I would find it very handy if there was a flag --all-schemas so I could run

oapi2ts api/file-with-just-schemas-like-above-example.yaml --all-schemas -o path/for/generated/stuff

and have it just emit whatever was in components.schemas in that file. There are also some ways you could improve this & make it more flexible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions