Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One spec in multiple files (data types logic override) - error of generating #1767

Closed
sonicWhale opened this issue Oct 23, 2018 · 4 comments · Fixed by #1799
Closed

One spec in multiple files (data types logic override) - error of generating #1767

sonicWhale opened this issue Oct 23, 2018 · 4 comments · Fixed by #1799
Assignees
Labels

Comments

@sonicWhale
Copy link

Problem statement

runtime error - when try to generate spec with several files

Swagger specification

For example i separate one file to several minimal files
api.swagger.yaml

---
swagger: '2.0'
info:
  version: "1.0.0"
  title: FHIR API
  description: |
    #### FHIR API
schemes:
- http
host: "fhir.test.lan"
basePath: /

parameters:
  patientID:
    name: patientID
    in: path
    description: "patient ID"
    type: string
    required: true

paths:
  /Patient/{patientID}:
    parameters:
    - $ref: "#/parameters/patientID"
    get:
      tags:
      - fhir
      produces:
      - application/fhir+json
      consumes:
      - application/fhir+json
      responses:
        200:
          $ref: "#/responses/Patient"
    put:
      tags:
      - fhir
      produces:
      - application/fhir+json
      consumes:
      - application/fhir+json
      responses:
        200:
          $ref: "#/responses/Patient"

responses:
  Patient:
    description: "Patient"
    schema:
      allOf:
      - $ref: 'Patient.yaml'

Object (patient) describes in different file
Patient.yaml

type: object
title: "Info about patient"
description: ""
properties:
      id:
        type: string
      identifier:
        type: array
        description: "Identifier"
        items:
            allOf:
            - $ref: 'Identifier.yaml'
      active:
        type: boolean
      name:
        description: "Name"
        type: string
required:
- name

Patient has Identifier which exist in another file
Identifier.yaml

type: object
title: "Identifier"
description: "v3.1.0-12545"
properties:
  use:
    type: string
    example: "usual"
    enum:
    - "usual"
    - "official"
    - "temp"
    - "secondary"
    - "old"
  system:
    type: string
  value:
    description: "Some unique value"
    type: string
  assigner:
    description: "Organization"
    allOf:
      - $ref: 'Reference.yaml'
required:
  - use

Identifier has field with type Reference which describes like
Reference.yaml

type: object
title: "reference"
description: "v3.1.0-12545"
properties:
      reference:
        description: "Literal reference, Relative, internal or absolute URL"
        type: string
        example: "literal reference"
      identifier:
        description: "Logical reference, when literal reference is not known"
        allOf:
        - $ref: 'Identifier.yaml'
      display:
        type: string
        description: "Text alternative for the resource"

One interesting thing than Reference has one field type of Identifier
all types in this example equal logic data types of FHIR format

Steps to reproduce

let's try to generate server

swagger generate server -f ./api.swagger.yaml --with-flatten=full --exclude-main

return error

2018/10/23 19:11:35 validating spec /Users/aj/go/src/awesomeProject3/api.swagger.yaml
2018/10/23 19:11:35 preprocessing spec with option:  full flattening
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x1a0 pc=0x13ad539]

goroutine 1 [running]:
github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis.(*AnalyzedSchema).initializeFlags(0xc4207fce00)
        /go/src/github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis/schema.go:213 +0x29
github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis.Schema(0x0, 0x16f1880, 0xc42116e700, 0xc420784bc0, 0x31, 0x0, 0xc420d8bc80, 0x0, 0x0)
        /go/src/github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis/schema.go:25 +0xa2
github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis.namePointers(0xc421196090, 0xc4215436b0, 0x138d369)
        /go/src/github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis/flatten.go:1317 +0xec9
github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis.stripPointersAndOAIGen(0xc421196090, 0x0, 0x0)
        /go/src/github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis/flatten.go:1130 +0x2f
github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis.Flatten(0xc420254000, 0x0, 0xc420784bc0, 0x31, 0x10000, 0x0, 0x0)
        /go/src/github.com/go-swagger/go-swagger/vendor/github.com/go-openapi/analysis/flatten.go:182 +0x31a
github.com/go-swagger/go-swagger/generator.validateAndFlattenSpec(0xc4204ba400, 0xc42085ba40, 0xc420784bc0, 0x31, 0xc42085ba40)
        /go/src/github.com/go-swagger/go-swagger/generator/shared.go:1073 +0x1dc
github.com/go-swagger/go-swagger/generator.newAppGenerator(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc4204ba400, 0xc4204c0120, ...)
        /go/src/github.com/go-swagger/go-swagger/generator/support.go:92 +0x162
github.com/go-swagger/go-swagger/generator.GenerateServer(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc4204ba400, 0xc4202b89c0, ...)
        /go/src/github.com/go-swagger/go-swagger/generator/support.go:39 +0x94
github.com/go-swagger/go-swagger/cmd/swagger/commands/generate.(*Server).generate(0xc420696680, 0xc4204ba400, 0x0, 0x0)
        /go/src/github.com/go-swagger/go-swagger/cmd/swagger/commands/generate/server.go:86 +0x94

Maybe this datatypes looks like something strange
Patient -> Identifier -> Reference -> Identifier - but this is standard
When all datatypes describes in one swagger file - all works ok but for more usability need to separate by several files
If in Reference description change type 'Identifier.yaml' for example - on string type - all generated good

How it can be fixed?
Thanks!

Environment

swagger version: 0.16.0
go version: 1.10.3
OS: mojave 10.14

@fredbi fredbi added the bug label Oct 23, 2018
@fredbi
Copy link
Contributor

fredbi commented Oct 23, 2018

interesting use case. Looks like a bug in go-openapi/analysis to flatten this spec.

You could try moving your ext references from responses to definitions, it might help.

@fredbi
Copy link
Contributor

fredbi commented Oct 24, 2018

Mmhhh. Tried to play a bit with this spec.

Problem lays with circular ref formed by several files. We still have a problem in go-openapi/spec with such circulars.

Try defining the cycle in one single file.

@sonicWhale
Copy link
Author

Mmhhh. Tried to play a bit with this spec.

Problem lays with circular ref formed by several files. We still have a problem in go-openapi/spec with such circulars.

Try defining the cycle in one single file.

If i describe "Patient" object in "definitions" zone then i get error

is invalid against swagger specification 2.0. see errors :
- definitions.Patient.schema in body is a forbidden property

Because one schema using in body and in responses

So in Identifier.yaml i set the value for reference type like full description object

type: object
title: "Identifier"
description: "v3.1.0-12545"
properties:
  use:
    type: string
    example: "usual"
    enum:
    - "usual"
    - "official"
    - "temp"
    - "secondary"
    - "old"
  type:
    description: "Identifier type"
    allOf:
      - $ref: 'CodeableConcept.yaml'
  system:
    type: string
    example: "urn:oid:1.2.36.146.595.217.0.1"
  value:
    description: "Some unique value"
    type: string
  period:
    description: "Time"
    allOf:
      - $ref : 'Period.yaml'
  assigner:
    description: "Organization"
    type: object
    title: "reference"
    description: "v3.1.0-12545"
    properties:
          reference:
            description: "Literal reference, Relative, internal or absolute URL"
            type: string
            example: "literal reference"
          identifier:
            description: "Logical reference, when literal reference is not known"
            type: array
            items:
              allOf:
              - $ref: 'Identifier.yaml'
          display:
            type: string
            description: "Text alternative for the resource"
required:
  - use

But when i'm trying to generate - return's me same runtime error

@fredbi fredbi added the spec label Oct 25, 2018
fredbi added a commit to fredbi/analysis that referenced this issue Nov 16, 2018
* Acknowleges go-swagger/go-swagger#1774

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/analysis that referenced this issue Nov 16, 2018
* Acknowleges go-swagger/go-swagger#1774

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@fredbi fredbi self-assigned this Nov 16, 2018
@fredbi fredbi added flatten Related to spec flattening and removed spec labels Nov 16, 2018
@fredbi
Copy link
Contributor

fredbi commented Nov 16, 2018

Works with the fix mentioned above. Please mark that this requires --with-flatten=full to generate a proper build (the minimal mode generates an invalid recursive go type)

fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 16, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 16, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 17, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 17, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Updates vendor from go-openapi/analysis

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 18, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Updates vendor from go-openapi/analysis

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 19, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Updates vendor from go-openapi/analysis

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 22, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Updates vendor from go-openapi/analysis

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Nov 28, 2018
* fixes go-swagger#1796
* fixes go-swagger#1774
* fixes go-swagger#1767

Updates vendor from go-openapi/analysis

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit that referenced this issue Nov 28, 2018
* Fix flattening spec with remote circular $ref

* fixes #1796
* fixes #1774
* fixes #1767

Updates vendor from go-openapi/analysis

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

* * Updates vendor (20181118)

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants