swagger-api / swagger-api/swagger-codegen

[html] codegen ignores schema on a separate path

Open
#4,768 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: HTML help wanted Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When parsing a swagger.yaml file which contains external schema via $ref, swagger-codegen only looks at the last element of the schema path. Thus, for the below definition only the schema for a at http://localhost/swagger-codegen-apidoc/schemas/a/schema.json is processed. The schema for b at http://localhost/swagger-codegen-apidoc/schemas/b/schema.json is ignored. Using -l html, the schema links for member b resolves to a model named schema which links to the schema for http://localhost/swagger-codegen-apidoc/schemas/a/schema.json. The name schema appears to be derived from the last path element schema.json and dropping everything after the first ..

Our real use case depends on the ability to distinguish models by path, as we use
the url pattern
http://www.example.com/schema/{{media-type-name}}/v1/schema.json
for schema for many different application/vnd.sas.* media types
as these media types / schema are shared across many APIs. (We do not
embed them in each swagger.yaml file but use $ref to reference the shared schema files.)

For example with generate -l html, the output looks like

model - A sample model
A model that which represents a thing resource. contains members a, b, and a version.
a (optional)
schema Model A
b (optional)
schema Model B
version (optional)
String semantic version

swagger-codegen also can't distinguish resources such as
http://localhost/swagger-codegen-apidoc/schemas/application/vnd.sas.a.json
and http://localhost/swagger-codegen-apidoc/schemas/application/vnd.sas.b.json.
swagger-codegen appear to strip off everything after the first . in the last path element,
and thus both of these resolve to the model name vnd.

The only way to make this work is to contort the resource names:
http://localhost/swagger-codegen-apidoc/schemas/application/vnd-sas-a.json
http://localhost/swagger-codegen-apidoc/schemas/application/vnd-sas-b.json
We would prefer to not do this just because of the tooling.

swagger-ui also has similar problems with these schema resources.

Swagger-codegen version

2.2.2-SNAPSHOT

Swagger declaration file content or url

swagger.yaml :

swagger: "2.0"
info:
  version: "1"
  title: A Sample API
  description: swagger tools ignore schema with different paths

basePath: '/sample'
paths:
  /thing:
    get:
      summary: Return a thing
      description: Return the JSON representation of a `thing` resource.
      produces:
        - application/json
      responses:
        '200':
          description: OK
          schema:
            $ref: '#/definitions/model'
definitions:
  model:
    title: A sample model
    description: A model that which represents a `thing` resource. contains members a, b, and a version.
    properties:
       a:
          description: Model **A**
          type: object
          $ref: http://localhost/swagger-codegen-apidoc/schemas/a/schema.json
       b:
          description: Model **B**
          type: object
          $ref: http://localhost/swagger-codegen-apidoc/schemas/b/schema.json
       version:
          description: "[semantic version](http://semver.org/)"
          type: string

schemas/a/schema.json :

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id" : "http://localhost/reference/schema/a/schema.json",
    "title": "Link",
    "description": "Model A",
    "properties": {
        "foo": {
            "type": "string",
            "description": "some data...."
        }
    }
}

schemas/b/schema.json :

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id" : "http://localhost/reference/schema/a/schema.json",
    "title": "Link",
    "description": "Model B",
    "properties": {
        "bar": {
            "type": "string",
            "description": "some other data...."
        }
    }
}

Command line used for generation
java -jar  ~/dev/swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar \
  generate -i http://localhost/swagger-codegen-apidoc/swagger.yaml -l html  -o samples/apidoc
Steps to reproduce

Configure localhost so that the directory containing the files is available via the context 'swagger-codegen-apidoc'

swagger.yaml
schemas/a/schema.json
schemas/b/schema.json
Related issues
Suggest a Fix

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 with the supplied swagger.yaml and the external schemas at schemas/a/schema.json and schemas/b/schema.json, then run the documented generate -l html command. Trace how external $ref paths become model names and compare the generated links for models A and B. Done means schemas with different paths or filenames remain distinct in the HTML output.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.