Skip to content

readOnly: true forbid re-use of ref in valid context #74

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

Closed
eLvErDe opened this issue Jan 13, 2021 · 5 comments · Fixed by #75
Closed

readOnly: true forbid re-use of ref in valid context #74

eLvErDe opened this issue Jan 13, 2021 · 5 comments · Fixed by #75

Comments

@eLvErDe
Copy link
Contributor

eLvErDe commented Jan 13, 2021

Hello again,

If I define a model for ObjectId which is marked as readOnly: true I can use it properly in GET and POST route (and thanks a lot for that !) sadly I cannot re-use this model in /get/{id} route (validation fails).

Here is a complete example showing the issue (actually most of the code is useless, all you need is to call /get/12345 and see the validation error:

import tempfile
from uuid import uuid4
from aiohttp import web

from aiohttp_swagger3 import SwaggerDocs, SwaggerUiSettings


data = {}

swagger_spec = """
components:
  schemas:
    ObjectId:
      type: string
      example: 123e4567-e89b-12d3-a456-426614174000
      readOnly: true
    SomeObject:
      type: object
      properties:
        id:
          $ref: '#/components/schemas/ObjectId'
        prop1:
          type: boolean
        prop2:
          type: string
      required:
      - id
      - prop1
      - prop2
"""


async def post(request, body):
    """
    Create a new object with readOnly id prop

    ---
    summary: Create a new object with id generated server side
    requestBody:
      required: true
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/SomeObject'
    responses:
      "200":
        description: Worked
    """

    uuid = str(uuid4())
    body["id"] = uuid
    data[uuid] = body
    return web.json_response(body)


async def get(request):
    """
    Get all objects stored in memory

    ---
    summary: Get all objects stored in memory
    responses:
      "200":
        description: Worked
    """

    return web.json_response(list(data.values()))


async def get_one(request, id):
    """
    Get object matching provided id

    ---
    summary: Get object matching provided id
    parameters:
    - name: id
      in: path
      required: true
      schema:
        $ref: '#/components/schemas/ObjectId'
    responses:
      "200":
        description: Worked
    """

    return web.json_response(data[id])


def main():
    app = web.Application()

    with tempfile.NamedTemporaryFile() as spec_fh:
        spec_fh.write(bytes(swagger_spec.strip(), "utf-8"))
        spec_fh.seek(0)
        swagger = SwaggerDocs(app, components=spec_fh.name, swagger_ui_settings=SwaggerUiSettings(path="/"))

    swagger.add_routes([web.post("/post", post), web.get("/get", get, allow_head=False), web.get("/get/{id}", get_one, allow_head=False)])

    web.run_app(app)


if __name__ == "__main__":
    main()

Regards, Adam.

@hh-h
Copy link
Owner

hh-h commented Jan 13, 2021

Hi, this is incorrect usage of readOnly, see docs

You can use the readOnly and writeOnly keywords to mark specific properties as read-only or write-only.

Relevant only for Schema "properties" definitions.

You can use it only in properties section.

@eLvErDe
Copy link
Contributor Author

eLvErDe commented Jan 13, 2021

Fair enough, stack overflow then advices to combine with allOf which will lead to the other issue I reported earlier :D

https://stackoverflow.com/a/51402417

@hh-h hh-h closed this as completed Jan 13, 2021
@eLvErDe
Copy link
Contributor Author

eLvErDe commented Jan 13, 2021

Btw, found more information about his:
OAI/OpenAPI-Specification#1622

If I understand correctly it's kinda a grey area but it seems to be common to just ignore this property when seen outside applicable context.

@hh-h
Copy link
Owner

hh-h commented Jan 14, 2021

I thought it will be hard to implement, but with the addition of a pinch of crutches, it is actually possible :)

@hh-h hh-h reopened this Jan 14, 2021
@hh-h hh-h mentioned this issue Jan 14, 2021
@hh-h hh-h closed this as completed in #75 Jan 14, 2021
@eLvErDe
Copy link
Contributor Author

eLvErDe commented Jan 14, 2021

Thanks :)

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

Successfully merging a pull request may close this issue.

2 participants