Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Resource types optional properties is not working with default media type. #143

Open
Prevole opened this issue Apr 20, 2015 · 1 comment

Comments

@Prevole
Copy link

Prevole commented Apr 20, 2015

Hi there,

I think I discovered an issue.

Having the following:

#%RAML 0.8

---
title: My Sample App
baseUri: http://somwhere.really.cool/{version}/
version: v1
protocols: [HTTPS]
mediaType: application/json

resourceTypes:
  - member: 
      usage: This resourceType should be used for any member of collection.
      get:
        responses:
          200:
            body:
              schema: <<resourcePathName | !singularize>>
              example: <<exampleGetResponse>>

      patch?:
        body:
          example: <<examplePatchRequest>>
          responses:
            201:
              headers:
                location:
                  type: string
                  example: /<<resourcePathName>>/1

/organizations:
  /{id}:
    type:
      member:
        exampleGetResponse: !include examples/organizations/response-get.json
        examplePatchRequest: !include examples/organizations/request-patch.json
    patch:

When the YAML file generated and a JSON is produced, the media type is not present for the method patch for path /organizations/{id}. If I remove the ?, the application/json appears in the parsed content:

Incorrect

...
"methods": [
    {
        "responses": {
            "200": {
                "body": {
                    "application/json": {
                        "schema": "{\n  \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n  \"title\": \"Organization\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"name\": {\n      \"type\": \"string\"\n    }\n  },\n  \"required\": [ \"name\" ]\n}",
                        "example": "{\n  \"name\": \"Awesome orga\"\n}"
                    }
                }
            }
        },
        "method": "get"
    },
    {
        "body": {
            "example": "#{\n  \"name\": \"Awesome orga\"\n}"
        },
        "responses": {
            "201": {
                "headers": {
                    "location": {
                        "type": "string",
                        "example": "/organizations/1",
                        "displayName": "location"
                    }
                }
            }
        },
        "method": "patch"
    }
]

Correct

...
"methods": [
    {
        "responses": {
            "200": {
                "body": {
                    "application/json": {
                        "schema": "{\n  \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n  \"title\": \"Organization\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"name\": {\n      \"type\": \"string\"\n    }\n  },\n  \"required\": [ \"name\" ]\n}",
                        "example": "{\n  \"name\": \"Awesome orga\"\n}"
                    }
                }
            }
        },
        "method": "get"
    },
    {
        "body": {
            "application/json": {
                "example": "#{\n  \"name\": \"Awesome orga\"\n}"
            }
        },
        "responses": {
            "201": {
                "headers": {
                    "location": {
                        "type": "string",
                        "example": "/organizations/1",
                        "displayName": "location"
                    }
                }
            }
        },
        "method": "patch"
    }
]

During my experimentation, I went to modify this file: https://github.com/raml-org/raml-js-parser/blob/master/src/resourceTypes.coffee#L54-L55

before

    tempType.combine resource[1]
    resource[1] = tempType

after

    tempType.combine resource[1]
    @apply_default_media_type_to_resource tempType
    resource[1] = tempType

Honestly, I really do not know exactly what I did there but it changed the parsed result in a way that the bodies now contain the default media type from the main RAML document when none are specified in my optional resources.

@dmartinezg
Copy link

hi @Prevole, good catch, I think this has to be done in @resolve_inheritance_chain which should return a fully expanded resource type

I will take a look and fix this, thanks for reporting

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants