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

Support for generating examples in MockResolver for... #1718

Closed
wants to merge 7 commits into from

Conversation

NanuakShadow
Copy link

@NanuakShadow NanuakShadow commented Jul 4, 2023

…string, number, integer, bool

Fixes # .

Changes proposed in this pull request:

  • Implements the generation of an example dependent on information provided in the case that an example isn't provided already.
  • Types included: strings, incl. maximum and minimum string length and regular expressions (with the use of an external dependency , rstr. Integers, incl. maximum and minimum values as well as their respective exclusive values, i.e. <, > over <=, >=. Boolean and numbers, both in a more simple state since they don't have much variations.

@NanuakShadow NanuakShadow changed the title Basic support for generating examples in MockResolver for string, num… WIP: Basic support for generating examples in MockResolver for string, num… Jul 4, 2023
@NanuakShadow NanuakShadow changed the title WIP: Basic support for generating examples in MockResolver for string, num… Support for generating examples in MockResolver for string, num… Jul 5, 2023
@NanuakShadow
Copy link
Author

@Ruwann @RobbeSneyders could you review or make any comments?

except KeyError:
pass
try:
return(generate_example(schema))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return(generate_example(schema))
return generate_example(schema)

except KeyError:
pass
try:
return(generate_example(schema))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return(generate_example(schema))
return generate_example(schema)

@@ -10,6 +10,7 @@
import pkgutil
import sys
import typing as t
from rstr import xeger
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an external dependency that will need to be added to https://github.com/spec-first/connexion/blob/main/pyproject.toml#L46.

It should possibly be optional (like flask and swagger-ui).

from re import fullmatch, Match


def test_short_string():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better name would be test_generate_example_string. And similar below.


example = generate_example(schema)

assert type(fullmatch(pattern, example)) == Match and isinstance(example, str)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fullmatch either returns a Match object or None. Instead of checking the if the returned type is Match, it is cleaner to do the inverse and check it is not None`.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I'd tried previously, it didn't work for some reason. It's working now though so I'll correct that.

@NanuakShadow NanuakShadow changed the title Support for generating examples in MockResolver for string, num… Support for generating examples in MockResolver for Jul 7, 2023
@NanuakShadow NanuakShadow changed the title Support for generating examples in MockResolver for Support for generating examples in MockResolver for... Jul 7, 2023
@RobbeSneyders
Copy link
Member

Thanks @NanuakShadow!

While I agree that generating example responses based on the schema would be useful, I don't want to take on the effort to maintain this in Connexion. This PR implements this logic only for the most simple schema's, which means that we'll get issues for every case where this is not working.

If there is a third-party library that can provide this behavior, I'm happy to integrate it.

@RobbeSneyders
Copy link
Member

Closing this based on my comment above.

@mjp4 mjp4 mentioned this pull request Feb 21, 2024
RobbeSneyders added a commit that referenced this pull request Mar 20, 2024
Fixes #1719 .

Builds on work of #1718,
pulling in external library to provide the fake data from JSON schema.

Changes proposed in this pull request:

* Use JSF library to generate sample data for mocking APIs without
examples.
* Add this as a new extra dependency "mock"

See discussion on #1870

---------

Co-authored-by: Robbe Sneyders <robbe.sneyders@gmail.com>
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 this pull request may close these issues.

3 participants